1 // SPDX-License-Identifier: GPL-2.0+
3 * PCI Hotplug Driver for PowerPC PowerNV platform.
5 * Copyright Gavin Shan, IBM Corporation 2016.
8 #include <linux/bitfield.h>
9 #include <linux/libfdt.h>
10 #include <linux/module.h>
11 #include <linux/pci.h>
12 #include <linux/pci_hotplug.h>
13 #include <linux/of_fdt.h>
16 #include <asm/pnv-pci.h>
17 #include <asm/ppc-pci.h>
19 #define DRIVER_VERSION "0.1"
20 #define DRIVER_AUTHOR "Gavin Shan, IBM Corporation"
21 #define DRIVER_DESC "PowerPC PowerNV PCI Hotplug Driver"
23 #define SLOT_WARN(sl, x...) \
24 ((sl)->pdev ? pci_warn((sl)->pdev, x) : dev_warn(&(sl)->bus->dev, x))
26 struct pnv_php_event {
28 struct pnv_php_slot *php_slot;
29 struct work_struct work;
32 static LIST_HEAD(pnv_php_slot_list);
33 static DEFINE_SPINLOCK(pnv_php_lock);
35 static void pnv_php_register(struct device_node *dn);
36 static void pnv_php_unregister_one(struct device_node *dn);
37 static void pnv_php_unregister(struct device_node *dn);
39 static void pnv_php_disable_irq(struct pnv_php_slot *php_slot,
42 struct pci_dev *pdev = php_slot->pdev;
43 int irq = php_slot->irq;
46 if (php_slot->irq > 0) {
47 pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &ctrl);
48 ctrl &= ~(PCI_EXP_SLTCTL_HPIE |
50 PCI_EXP_SLTCTL_DLLSCE);
51 pcie_capability_write_word(pdev, PCI_EXP_SLTCTL, ctrl);
53 free_irq(php_slot->irq, php_slot);
58 destroy_workqueue(php_slot->wq);
62 if (disable_device || irq > 0) {
63 if (pdev->msix_enabled)
64 pci_disable_msix(pdev);
65 else if (pdev->msi_enabled)
66 pci_disable_msi(pdev);
68 pci_disable_device(pdev);
72 static void pnv_php_free_slot(struct kref *kref)
74 struct pnv_php_slot *php_slot = container_of(kref,
75 struct pnv_php_slot, kref);
77 WARN_ON(!list_empty(&php_slot->children));
78 pnv_php_disable_irq(php_slot, false);
79 kfree(php_slot->name);
83 static inline void pnv_php_put_slot(struct pnv_php_slot *php_slot)
89 kref_put(&php_slot->kref, pnv_php_free_slot);
92 static struct pnv_php_slot *pnv_php_match(struct device_node *dn,
93 struct pnv_php_slot *php_slot)
95 struct pnv_php_slot *target, *tmp;
97 if (php_slot->dn == dn) {
98 kref_get(&php_slot->kref);
102 list_for_each_entry(tmp, &php_slot->children, link) {
103 target = pnv_php_match(dn, tmp);
111 struct pnv_php_slot *pnv_php_find_slot(struct device_node *dn)
113 struct pnv_php_slot *php_slot, *tmp;
116 spin_lock_irqsave(&pnv_php_lock, flags);
117 list_for_each_entry(tmp, &pnv_php_slot_list, link) {
118 php_slot = pnv_php_match(dn, tmp);
120 spin_unlock_irqrestore(&pnv_php_lock, flags);
124 spin_unlock_irqrestore(&pnv_php_lock, flags);
128 EXPORT_SYMBOL_GPL(pnv_php_find_slot);
131 * Remove pdn for all children of the indicated device node.
132 * The function should remove pdn in a depth-first manner.
134 static void pnv_php_rmv_pdns(struct device_node *dn)
136 struct device_node *child;
138 for_each_child_of_node(dn, child) {
139 pnv_php_rmv_pdns(child);
141 pci_remove_device_node_info(child);
146 * Detach all child nodes of the indicated device nodes. The
147 * function should handle device nodes in depth-first manner.
149 * We should not invoke of_node_release() as the memory for
150 * individual device node is part of large memory block. The
151 * large block is allocated from memblock (system bootup) or
152 * kmalloc() when unflattening the device tree by OF changeset.
153 * We can not free the large block allocated from memblock. For
154 * later case, it should be released at once.
156 static void pnv_php_detach_device_nodes(struct device_node *parent)
158 struct device_node *dn;
160 for_each_child_of_node(parent, dn) {
161 pnv_php_detach_device_nodes(dn);
168 static void pnv_php_rmv_devtree(struct pnv_php_slot *php_slot)
170 pnv_php_rmv_pdns(php_slot->dn);
173 * Decrease the refcount if the device nodes were created
174 * through OF changeset before detaching them.
177 of_changeset_destroy(&php_slot->ocs);
178 pnv_php_detach_device_nodes(php_slot->dn);
182 kfree(php_slot->fdt);
184 php_slot->dn->child = NULL;
185 php_slot->fdt = NULL;
190 * As the nodes in OF changeset are applied in reverse order, we
191 * need revert the nodes in advance so that we have correct node
192 * order after the changeset is applied.
194 static void pnv_php_reverse_nodes(struct device_node *parent)
196 struct device_node *child, *next;
199 for_each_child_of_node(parent, child)
200 pnv_php_reverse_nodes(child);
202 /* Reverse the nodes in the child list */
203 child = parent->child;
204 parent->child = NULL;
206 next = child->sibling;
208 child->sibling = parent->child;
209 parent->child = child;
214 static int pnv_php_populate_changeset(struct of_changeset *ocs,
215 struct device_node *dn)
217 struct device_node *child;
220 for_each_child_of_node(dn, child) {
221 ret = of_changeset_attach_node(ocs, child);
227 ret = pnv_php_populate_changeset(ocs, child);
237 static void *pnv_php_add_one_pdn(struct device_node *dn, void *data)
239 struct pci_controller *hose = (struct pci_controller *)data;
242 pdn = pci_add_device_node_info(hose, dn);
244 return ERR_PTR(-ENOMEM);
249 static void pnv_php_add_pdns(struct pnv_php_slot *slot)
251 struct pci_controller *hose = pci_bus_to_host(slot->bus);
253 pci_traverse_device_nodes(slot->dn, pnv_php_add_one_pdn, hose);
256 static int pnv_php_add_devtree(struct pnv_php_slot *php_slot)
258 void *fdt, *fdt1, *dt;
261 /* We don't know the FDT blob size. We try to get it through
262 * maximal memory chunk and then copy it to another chunk that
263 * fits the real size.
265 fdt1 = kzalloc(0x10000, GFP_KERNEL);
271 ret = pnv_pci_get_device_tree(php_slot->dn->phandle, fdt1, 0x10000);
273 SLOT_WARN(php_slot, "Error %d getting FDT blob\n", ret);
277 fdt = kmemdup(fdt1, fdt_totalsize(fdt1), GFP_KERNEL);
283 /* Unflatten device tree blob */
284 dt = of_fdt_unflatten_tree(fdt, php_slot->dn, NULL);
287 SLOT_WARN(php_slot, "Cannot unflatten FDT\n");
291 /* Initialize and apply the changeset */
292 of_changeset_init(&php_slot->ocs);
293 pnv_php_reverse_nodes(php_slot->dn);
294 ret = pnv_php_populate_changeset(&php_slot->ocs, php_slot->dn);
296 pnv_php_reverse_nodes(php_slot->dn);
297 SLOT_WARN(php_slot, "Error %d populating changeset\n",
302 php_slot->dn->child = NULL;
303 ret = of_changeset_apply(&php_slot->ocs);
305 SLOT_WARN(php_slot, "Error %d applying changeset\n", ret);
306 goto destroy_changeset;
309 /* Add device node firmware data */
310 pnv_php_add_pdns(php_slot);
317 of_changeset_destroy(&php_slot->ocs);
320 php_slot->dn->child = NULL;
329 static inline struct pnv_php_slot *to_pnv_php_slot(struct hotplug_slot *slot)
331 return container_of(slot, struct pnv_php_slot, slot);
334 int pnv_php_set_slot_power_state(struct hotplug_slot *slot,
337 struct pnv_php_slot *php_slot = to_pnv_php_slot(slot);
341 ret = pnv_pci_set_power_state(php_slot->id, state, &msg);
343 if (be64_to_cpu(msg.params[1]) != php_slot->dn->phandle ||
344 be64_to_cpu(msg.params[2]) != state) {
345 SLOT_WARN(php_slot, "Wrong msg (%lld, %lld, %lld)\n",
346 be64_to_cpu(msg.params[1]),
347 be64_to_cpu(msg.params[2]),
348 be64_to_cpu(msg.params[3]));
351 if (be64_to_cpu(msg.params[3]) != OPAL_SUCCESS) {
355 } else if (ret < 0) {
359 if (state == OPAL_PCI_SLOT_POWER_OFF || state == OPAL_PCI_SLOT_OFFLINE)
360 pnv_php_rmv_devtree(php_slot);
362 ret = pnv_php_add_devtree(php_slot);
367 SLOT_WARN(php_slot, "Error %d powering %s\n",
368 ret, (state == OPAL_PCI_SLOT_POWER_ON) ? "on" : "off");
371 EXPORT_SYMBOL_GPL(pnv_php_set_slot_power_state);
373 static int pnv_php_get_power_state(struct hotplug_slot *slot, u8 *state)
375 struct pnv_php_slot *php_slot = to_pnv_php_slot(slot);
376 uint8_t power_state = OPAL_PCI_SLOT_POWER_ON;
380 * Retrieve power status from firmware. If we fail
381 * getting that, the power status fails back to
384 ret = pnv_pci_get_power_state(php_slot->id, &power_state);
386 SLOT_WARN(php_slot, "Error %d getting power status\n",
389 *state = power_state;
395 static int pnv_php_get_adapter_state(struct hotplug_slot *slot, u8 *state)
397 struct pnv_php_slot *php_slot = to_pnv_php_slot(slot);
398 uint8_t presence = OPAL_PCI_SLOT_EMPTY;
402 * Retrieve presence status from firmware. If we can't
403 * get that, it will fail back to be empty.
405 ret = pnv_pci_get_presence_state(php_slot->id, &presence);
410 SLOT_WARN(php_slot, "Error %d getting presence\n", ret);
416 static int pnv_php_get_attention_state(struct hotplug_slot *slot, u8 *state)
418 struct pnv_php_slot *php_slot = to_pnv_php_slot(slot);
420 *state = php_slot->attention_state;
424 static int pnv_php_set_attention_state(struct hotplug_slot *slot, u8 state)
426 struct pnv_php_slot *php_slot = to_pnv_php_slot(slot);
427 struct pci_dev *bridge = php_slot->pdev;
430 php_slot->attention_state = state;
434 mask = PCI_EXP_SLTCTL_AIC;
437 new = PCI_EXP_SLTCTL_ATTN_IND_ON;
439 new = PCI_EXP_SLTCTL_ATTN_IND_OFF;
441 pcie_capability_clear_and_set_word(bridge, PCI_EXP_SLTCTL, mask, new);
446 static int pnv_php_enable(struct pnv_php_slot *php_slot, bool rescan)
448 struct hotplug_slot *slot = &php_slot->slot;
449 uint8_t presence = OPAL_PCI_SLOT_EMPTY;
450 uint8_t power_status = OPAL_PCI_SLOT_POWER_ON;
453 /* Check if the slot has been configured */
454 if (php_slot->state != PNV_PHP_STATE_REGISTERED)
457 /* Retrieve slot presence status */
458 ret = pnv_php_get_adapter_state(slot, &presence);
463 * Proceed if there have nothing behind the slot. However,
464 * we should leave the slot in registered state at the
465 * beginning. Otherwise, the PCI devices inserted afterwards
466 * won't be probed and populated.
468 if (presence == OPAL_PCI_SLOT_EMPTY) {
469 if (!php_slot->power_state_check) {
470 php_slot->power_state_check = true;
479 * If the power supply to the slot is off, we can't detect
480 * adapter presence state. That means we have to turn the
481 * slot on before going to probe slot's presence state.
483 * On the first time, we don't change the power status to
484 * boost system boot with assumption that the firmware
485 * supplies consistent slot power status: empty slot always
486 * has its power off and non-empty slot has its power on.
488 if (!php_slot->power_state_check) {
489 php_slot->power_state_check = true;
491 ret = pnv_php_get_power_state(slot, &power_status);
495 if (power_status != OPAL_PCI_SLOT_POWER_ON)
499 /* Check the power status. Scan the slot if it is already on */
500 ret = pnv_php_get_power_state(slot, &power_status);
504 if (power_status == OPAL_PCI_SLOT_POWER_ON)
507 /* Power is off, turn it on and then scan the slot */
508 ret = pnv_php_set_slot_power_state(slot, OPAL_PCI_SLOT_POWER_ON);
513 if (presence == OPAL_PCI_SLOT_PRESENT) {
515 pci_lock_rescan_remove();
516 pci_hp_add_devices(php_slot->bus);
517 pci_unlock_rescan_remove();
520 /* Rescan for child hotpluggable slots */
521 php_slot->state = PNV_PHP_STATE_POPULATED;
523 pnv_php_register(php_slot->dn);
525 php_slot->state = PNV_PHP_STATE_POPULATED;
531 static int pnv_php_reset_slot(struct hotplug_slot *slot, bool probe)
533 struct pnv_php_slot *php_slot = to_pnv_php_slot(slot);
534 struct pci_dev *bridge = php_slot->pdev;
538 * The CAPI folks want pnv_php to drive OpenCAPI slots
539 * which don't have a bridge. Only claim to support
540 * reset_slot() if we have a bridge device (for now...)
545 /* mask our interrupt while resetting the bridge */
546 if (php_slot->irq > 0)
547 disable_irq(php_slot->irq);
549 pci_bridge_secondary_bus_reset(bridge);
551 /* clear any state changes that happened due to the reset */
552 pcie_capability_read_word(php_slot->pdev, PCI_EXP_SLTSTA, &sts);
553 sts &= (PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_DLLSC);
554 pcie_capability_write_word(php_slot->pdev, PCI_EXP_SLTSTA, sts);
556 if (php_slot->irq > 0)
557 enable_irq(php_slot->irq);
562 static int pnv_php_enable_slot(struct hotplug_slot *slot)
564 struct pnv_php_slot *php_slot = to_pnv_php_slot(slot);
566 return pnv_php_enable(php_slot, true);
569 static int pnv_php_disable_slot(struct hotplug_slot *slot)
571 struct pnv_php_slot *php_slot = to_pnv_php_slot(slot);
575 * Allow to disable a slot already in the registered state to
576 * cover cases where the slot couldn't be enabled and never
577 * reached the populated state
579 if (php_slot->state != PNV_PHP_STATE_POPULATED &&
580 php_slot->state != PNV_PHP_STATE_REGISTERED)
583 /* Remove all devices behind the slot */
584 pci_lock_rescan_remove();
585 pci_hp_remove_devices(php_slot->bus);
586 pci_unlock_rescan_remove();
588 /* Detach the child hotpluggable slots */
589 pnv_php_unregister(php_slot->dn);
591 /* Notify firmware and remove device nodes */
592 ret = pnv_php_set_slot_power_state(slot, OPAL_PCI_SLOT_POWER_OFF);
594 php_slot->state = PNV_PHP_STATE_REGISTERED;
598 static const struct hotplug_slot_ops php_slot_ops = {
599 .get_power_status = pnv_php_get_power_state,
600 .get_adapter_status = pnv_php_get_adapter_state,
601 .get_attention_status = pnv_php_get_attention_state,
602 .set_attention_status = pnv_php_set_attention_state,
603 .enable_slot = pnv_php_enable_slot,
604 .disable_slot = pnv_php_disable_slot,
605 .reset_slot = pnv_php_reset_slot,
608 static void pnv_php_release(struct pnv_php_slot *php_slot)
612 /* Remove from global or child list */
613 spin_lock_irqsave(&pnv_php_lock, flags);
614 list_del(&php_slot->link);
615 spin_unlock_irqrestore(&pnv_php_lock, flags);
617 /* Detach from parent */
618 pnv_php_put_slot(php_slot);
619 pnv_php_put_slot(php_slot->parent);
622 static struct pnv_php_slot *pnv_php_alloc_slot(struct device_node *dn)
624 struct pnv_php_slot *php_slot;
630 ret = of_property_read_string(dn, "ibm,slot-label", &label);
634 if (pnv_pci_get_slot_id(dn, &id))
637 bus = pci_find_bus_by_node(dn);
641 php_slot = kzalloc(sizeof(*php_slot), GFP_KERNEL);
645 php_slot->name = kstrdup(label, GFP_KERNEL);
646 if (!php_slot->name) {
651 if (dn->child && PCI_DN(dn->child))
652 php_slot->slot_no = PCI_SLOT(PCI_DN(dn->child)->devfn);
654 php_slot->slot_no = -1; /* Placeholder slot */
656 kref_init(&php_slot->kref);
657 php_slot->state = PNV_PHP_STATE_INITIALIZED;
659 php_slot->pdev = bus->self;
662 php_slot->power_state_check = false;
663 php_slot->slot.ops = &php_slot_ops;
665 INIT_LIST_HEAD(&php_slot->children);
666 INIT_LIST_HEAD(&php_slot->link);
671 static int pnv_php_register_slot(struct pnv_php_slot *php_slot)
673 struct pnv_php_slot *parent;
674 struct device_node *dn = php_slot->dn;
678 /* Check if the slot is registered or not */
679 parent = pnv_php_find_slot(php_slot->dn);
681 pnv_php_put_slot(parent);
685 /* Register PCI slot */
686 ret = pci_hp_register(&php_slot->slot, php_slot->bus,
687 php_slot->slot_no, php_slot->name);
689 SLOT_WARN(php_slot, "Error %d registering slot\n", ret);
693 /* Attach to the parent's child list or global list */
694 while ((dn = of_get_parent(dn))) {
700 parent = pnv_php_find_slot(dn);
709 spin_lock_irqsave(&pnv_php_lock, flags);
710 php_slot->parent = parent;
712 list_add_tail(&php_slot->link, &parent->children);
714 list_add_tail(&php_slot->link, &pnv_php_slot_list);
715 spin_unlock_irqrestore(&pnv_php_lock, flags);
717 php_slot->state = PNV_PHP_STATE_REGISTERED;
721 static int pnv_php_enable_msix(struct pnv_php_slot *php_slot)
723 struct pci_dev *pdev = php_slot->pdev;
724 struct msix_entry entry;
728 /* Get total number of MSIx entries */
729 nr_entries = pci_msix_vec_count(pdev);
733 /* Check hotplug MSIx entry is in range */
734 pcie_capability_read_word(pdev, PCI_EXP_FLAGS, &pcie_flag);
735 entry.entry = FIELD_GET(PCI_EXP_FLAGS_IRQ, pcie_flag);
736 if (entry.entry >= nr_entries)
740 ret = pci_enable_msix_exact(pdev, &entry, 1);
742 SLOT_WARN(php_slot, "Error %d enabling MSIx\n", ret);
749 static void pnv_php_event_handler(struct work_struct *work)
751 struct pnv_php_event *event =
752 container_of(work, struct pnv_php_event, work);
753 struct pnv_php_slot *php_slot = event->php_slot;
756 pnv_php_enable_slot(&php_slot->slot);
758 pnv_php_disable_slot(&php_slot->slot);
763 static irqreturn_t pnv_php_interrupt(int irq, void *data)
765 struct pnv_php_slot *php_slot = data;
766 struct pci_dev *pchild, *pdev = php_slot->pdev;
767 struct eeh_dev *edev;
769 struct pnv_php_event *event;
776 pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &sts);
777 sts &= (PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_DLLSC);
778 pcie_capability_write_word(pdev, PCI_EXP_SLTSTA, sts);
780 pci_dbg(pdev, "PCI slot [%s]: HP int! DLAct: %d, PresDet: %d\n",
782 !!(sts & PCI_EXP_SLTSTA_DLLSC),
783 !!(sts & PCI_EXP_SLTSTA_PDC));
785 if (sts & PCI_EXP_SLTSTA_DLLSC) {
786 pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lsts);
787 added = !!(lsts & PCI_EXP_LNKSTA_DLLLA);
788 } else if (!(php_slot->flags & PNV_PHP_FLAG_BROKEN_PDC) &&
789 (sts & PCI_EXP_SLTSTA_PDC)) {
790 ret = pnv_pci_get_presence_state(php_slot->id, &presence);
793 "PCI slot [%s] error %d getting presence (0x%04x), to retry the operation.\n",
794 php_slot->name, ret, sts);
798 added = !!(presence == OPAL_PCI_SLOT_PRESENT);
800 pci_dbg(pdev, "PCI slot [%s]: Spurious IRQ?\n", php_slot->name);
804 /* Freeze the removed PE to avoid unexpected error reporting */
806 pchild = list_first_entry_or_null(&php_slot->bus->devices,
807 struct pci_dev, bus_list);
808 edev = pchild ? pci_dev_to_eeh_dev(pchild) : NULL;
809 pe = edev ? edev->pe : NULL;
811 eeh_serialize_lock(&flags);
812 eeh_pe_mark_isolated(pe);
813 eeh_serialize_unlock(flags);
814 eeh_pe_set_option(pe, EEH_OPT_FREEZE_PE);
819 * The PE is left in frozen state if the event is missed. It's
820 * fine as the PCI devices (PE) aren't functional any more.
822 event = kzalloc(sizeof(*event), GFP_ATOMIC);
825 "PCI slot [%s] missed hotplug event 0x%04x\n",
826 php_slot->name, sts);
830 pci_info(pdev, "PCI slot [%s] %s (IRQ: %d)\n",
831 php_slot->name, added ? "added" : "removed", irq);
832 INIT_WORK(&event->work, pnv_php_event_handler);
833 event->added = added;
834 event->php_slot = php_slot;
835 queue_work(php_slot->wq, &event->work);
840 static void pnv_php_init_irq(struct pnv_php_slot *php_slot, int irq)
842 struct pci_dev *pdev = php_slot->pdev;
847 /* Allocate workqueue */
848 php_slot->wq = alloc_workqueue("pciehp-%s", 0, 0, php_slot->name);
850 SLOT_WARN(php_slot, "Cannot alloc workqueue\n");
851 pnv_php_disable_irq(php_slot, true);
855 /* Check PDC (Presence Detection Change) is broken or not */
856 ret = of_property_read_u32(php_slot->dn, "ibm,slot-broken-pdc",
858 if (!ret && broken_pdc)
859 php_slot->flags |= PNV_PHP_FLAG_BROKEN_PDC;
861 /* Clear pending interrupts */
862 pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &sts);
863 if (php_slot->flags & PNV_PHP_FLAG_BROKEN_PDC)
864 sts |= PCI_EXP_SLTSTA_DLLSC;
866 sts |= (PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_DLLSC);
867 pcie_capability_write_word(pdev, PCI_EXP_SLTSTA, sts);
869 /* Request the interrupt */
870 ret = request_irq(irq, pnv_php_interrupt, IRQF_SHARED,
871 php_slot->name, php_slot);
873 pnv_php_disable_irq(php_slot, true);
874 SLOT_WARN(php_slot, "Error %d enabling IRQ %d\n", ret, irq);
878 /* Enable the interrupts */
879 pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &ctrl);
880 if (php_slot->flags & PNV_PHP_FLAG_BROKEN_PDC) {
881 ctrl &= ~PCI_EXP_SLTCTL_PDCE;
882 ctrl |= (PCI_EXP_SLTCTL_HPIE |
883 PCI_EXP_SLTCTL_DLLSCE);
885 ctrl |= (PCI_EXP_SLTCTL_HPIE |
886 PCI_EXP_SLTCTL_PDCE |
887 PCI_EXP_SLTCTL_DLLSCE);
889 pcie_capability_write_word(pdev, PCI_EXP_SLTCTL, ctrl);
891 /* The interrupt is initialized successfully when @irq is valid */
895 static void pnv_php_enable_irq(struct pnv_php_slot *php_slot)
897 struct pci_dev *pdev = php_slot->pdev;
901 * The MSI/MSIx interrupt might have been occupied by other
902 * drivers. Don't populate the surprise hotplug capability
905 if (pci_dev_msi_enabled(pdev))
908 ret = pci_enable_device(pdev);
910 SLOT_WARN(php_slot, "Error %d enabling device\n", ret);
914 pci_set_master(pdev);
916 /* Enable MSIx interrupt */
917 irq = pnv_php_enable_msix(php_slot);
919 pnv_php_init_irq(php_slot, irq);
924 * Use MSI if MSIx doesn't work. Fail back to legacy INTx
925 * if MSI doesn't work either
927 ret = pci_enable_msi(pdev);
928 if (!ret || pdev->irq) {
930 pnv_php_init_irq(php_slot, irq);
934 static int pnv_php_register_one(struct device_node *dn)
936 struct pnv_php_slot *php_slot;
940 /* Check if it's hotpluggable slot */
941 ret = of_property_read_u32(dn, "ibm,slot-pluggable", &prop32);
945 ret = of_property_read_u32(dn, "ibm,reset-by-firmware", &prop32);
949 php_slot = pnv_php_alloc_slot(dn);
953 ret = pnv_php_register_slot(php_slot);
957 ret = pnv_php_enable(php_slot, false);
959 goto unregister_slot;
961 /* Enable interrupt if the slot supports surprise hotplug */
962 ret = of_property_read_u32(dn, "ibm,slot-surprise-pluggable", &prop32);
964 pnv_php_enable_irq(php_slot);
969 pnv_php_unregister_one(php_slot->dn);
971 pnv_php_put_slot(php_slot);
975 static void pnv_php_register(struct device_node *dn)
977 struct device_node *child;
980 * The parent slots should be registered before their
983 for_each_child_of_node(dn, child) {
984 pnv_php_register_one(child);
985 pnv_php_register(child);
989 static void pnv_php_unregister_one(struct device_node *dn)
991 struct pnv_php_slot *php_slot;
993 php_slot = pnv_php_find_slot(dn);
997 php_slot->state = PNV_PHP_STATE_OFFLINE;
998 pci_hp_deregister(&php_slot->slot);
999 pnv_php_release(php_slot);
1000 pnv_php_put_slot(php_slot);
1003 static void pnv_php_unregister(struct device_node *dn)
1005 struct device_node *child;
1007 /* The child slots should go before their parent slots */
1008 for_each_child_of_node(dn, child) {
1009 pnv_php_unregister(child);
1010 pnv_php_unregister_one(child);
1014 static int __init pnv_php_init(void)
1016 struct device_node *dn;
1018 pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
1019 for_each_compatible_node(dn, NULL, "ibm,ioda2-phb")
1020 pnv_php_register(dn);
1022 for_each_compatible_node(dn, NULL, "ibm,ioda3-phb")
1023 pnv_php_register(dn);
1025 for_each_compatible_node(dn, NULL, "ibm,ioda2-npu2-opencapi-phb")
1026 pnv_php_register_one(dn); /* slot directly under the PHB */
1030 static void __exit pnv_php_exit(void)
1032 struct device_node *dn;
1034 for_each_compatible_node(dn, NULL, "ibm,ioda2-phb")
1035 pnv_php_unregister(dn);
1037 for_each_compatible_node(dn, NULL, "ibm,ioda3-phb")
1038 pnv_php_unregister(dn);
1040 for_each_compatible_node(dn, NULL, "ibm,ioda2-npu2-opencapi-phb")
1041 pnv_php_unregister_one(dn); /* slot directly under the PHB */
1044 module_init(pnv_php_init);
1045 module_exit(pnv_php_exit);
1047 MODULE_VERSION(DRIVER_VERSION);
1048 MODULE_LICENSE("GPL v2");
1049 MODULE_AUTHOR(DRIVER_AUTHOR);
1050 MODULE_DESCRIPTION(DRIVER_DESC);