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/libfdt.h>
9 #include <linux/module.h>
10 #include <linux/pci.h>
11 #include <linux/pci_hotplug.h>
14 #include <asm/pnv-pci.h>
15 #include <asm/ppc-pci.h>
17 #define DRIVER_VERSION "0.1"
18 #define DRIVER_AUTHOR "Gavin Shan, IBM Corporation"
19 #define DRIVER_DESC "PowerPC PowerNV PCI Hotplug Driver"
21 #define SLOT_WARN(sl, x...) \
22 ((sl)->pdev ? pci_warn((sl)->pdev, x) : dev_warn(&(sl)->bus->dev, x))
24 struct pnv_php_event
{
26 struct pnv_php_slot
*php_slot
;
27 struct work_struct work
;
30 static LIST_HEAD(pnv_php_slot_list
);
31 static DEFINE_SPINLOCK(pnv_php_lock
);
33 static void pnv_php_register(struct device_node
*dn
);
34 static void pnv_php_unregister_one(struct device_node
*dn
);
35 static void pnv_php_unregister(struct device_node
*dn
);
37 static void pnv_php_disable_irq(struct pnv_php_slot
*php_slot
,
40 struct pci_dev
*pdev
= php_slot
->pdev
;
41 int irq
= php_slot
->irq
;
44 if (php_slot
->irq
> 0) {
45 pcie_capability_read_word(pdev
, PCI_EXP_SLTCTL
, &ctrl
);
46 ctrl
&= ~(PCI_EXP_SLTCTL_HPIE
|
48 PCI_EXP_SLTCTL_DLLSCE
);
49 pcie_capability_write_word(pdev
, PCI_EXP_SLTCTL
, ctrl
);
51 free_irq(php_slot
->irq
, php_slot
);
56 destroy_workqueue(php_slot
->wq
);
60 if (disable_device
|| irq
> 0) {
61 if (pdev
->msix_enabled
)
62 pci_disable_msix(pdev
);
63 else if (pdev
->msi_enabled
)
64 pci_disable_msi(pdev
);
66 pci_disable_device(pdev
);
70 static void pnv_php_free_slot(struct kref
*kref
)
72 struct pnv_php_slot
*php_slot
= container_of(kref
,
73 struct pnv_php_slot
, kref
);
75 WARN_ON(!list_empty(&php_slot
->children
));
76 pnv_php_disable_irq(php_slot
, false);
77 kfree(php_slot
->name
);
81 static inline void pnv_php_put_slot(struct pnv_php_slot
*php_slot
)
87 kref_put(&php_slot
->kref
, pnv_php_free_slot
);
90 static struct pnv_php_slot
*pnv_php_match(struct device_node
*dn
,
91 struct pnv_php_slot
*php_slot
)
93 struct pnv_php_slot
*target
, *tmp
;
95 if (php_slot
->dn
== dn
) {
96 kref_get(&php_slot
->kref
);
100 list_for_each_entry(tmp
, &php_slot
->children
, link
) {
101 target
= pnv_php_match(dn
, tmp
);
109 struct pnv_php_slot
*pnv_php_find_slot(struct device_node
*dn
)
111 struct pnv_php_slot
*php_slot
, *tmp
;
114 spin_lock_irqsave(&pnv_php_lock
, flags
);
115 list_for_each_entry(tmp
, &pnv_php_slot_list
, link
) {
116 php_slot
= pnv_php_match(dn
, tmp
);
118 spin_unlock_irqrestore(&pnv_php_lock
, flags
);
122 spin_unlock_irqrestore(&pnv_php_lock
, flags
);
126 EXPORT_SYMBOL_GPL(pnv_php_find_slot
);
129 * Remove pdn for all children of the indicated device node.
130 * The function should remove pdn in a depth-first manner.
132 static void pnv_php_rmv_pdns(struct device_node
*dn
)
134 struct device_node
*child
;
136 for_each_child_of_node(dn
, child
) {
137 pnv_php_rmv_pdns(child
);
139 pci_remove_device_node_info(child
);
144 * Detach all child nodes of the indicated device nodes. The
145 * function should handle device nodes in depth-first manner.
147 * We should not invoke of_node_release() as the memory for
148 * individual device node is part of large memory block. The
149 * large block is allocated from memblock (system bootup) or
150 * kmalloc() when unflattening the device tree by OF changeset.
151 * We can not free the large block allocated from memblock. For
152 * later case, it should be released at once.
154 static void pnv_php_detach_device_nodes(struct device_node
*parent
)
156 struct device_node
*dn
;
158 for_each_child_of_node(parent
, dn
) {
159 pnv_php_detach_device_nodes(dn
);
166 static void pnv_php_rmv_devtree(struct pnv_php_slot
*php_slot
)
168 pnv_php_rmv_pdns(php_slot
->dn
);
171 * Decrease the refcount if the device nodes were created
172 * through OF changeset before detaching them.
175 of_changeset_destroy(&php_slot
->ocs
);
176 pnv_php_detach_device_nodes(php_slot
->dn
);
180 kfree(php_slot
->fdt
);
182 php_slot
->dn
->child
= NULL
;
183 php_slot
->fdt
= NULL
;
188 * As the nodes in OF changeset are applied in reverse order, we
189 * need revert the nodes in advance so that we have correct node
190 * order after the changeset is applied.
192 static void pnv_php_reverse_nodes(struct device_node
*parent
)
194 struct device_node
*child
, *next
;
197 for_each_child_of_node(parent
, child
)
198 pnv_php_reverse_nodes(child
);
200 /* Reverse the nodes in the child list */
201 child
= parent
->child
;
202 parent
->child
= NULL
;
204 next
= child
->sibling
;
206 child
->sibling
= parent
->child
;
207 parent
->child
= child
;
212 static int pnv_php_populate_changeset(struct of_changeset
*ocs
,
213 struct device_node
*dn
)
215 struct device_node
*child
;
218 for_each_child_of_node(dn
, child
) {
219 ret
= of_changeset_attach_node(ocs
, child
);
225 ret
= pnv_php_populate_changeset(ocs
, child
);
235 static void *pnv_php_add_one_pdn(struct device_node
*dn
, void *data
)
237 struct pci_controller
*hose
= (struct pci_controller
*)data
;
240 pdn
= pci_add_device_node_info(hose
, dn
);
242 return ERR_PTR(-ENOMEM
);
247 static void pnv_php_add_pdns(struct pnv_php_slot
*slot
)
249 struct pci_controller
*hose
= pci_bus_to_host(slot
->bus
);
251 pci_traverse_device_nodes(slot
->dn
, pnv_php_add_one_pdn
, hose
);
254 static int pnv_php_add_devtree(struct pnv_php_slot
*php_slot
)
256 void *fdt
, *fdt1
, *dt
;
259 /* We don't know the FDT blob size. We try to get it through
260 * maximal memory chunk and then copy it to another chunk that
261 * fits the real size.
263 fdt1
= kzalloc(0x10000, GFP_KERNEL
);
269 ret
= pnv_pci_get_device_tree(php_slot
->dn
->phandle
, fdt1
, 0x10000);
271 SLOT_WARN(php_slot
, "Error %d getting FDT blob\n", ret
);
275 fdt
= kmemdup(fdt1
, fdt_totalsize(fdt1
), GFP_KERNEL
);
281 /* Unflatten device tree blob */
282 dt
= of_fdt_unflatten_tree(fdt
, php_slot
->dn
, NULL
);
285 SLOT_WARN(php_slot
, "Cannot unflatten FDT\n");
289 /* Initialize and apply the changeset */
290 of_changeset_init(&php_slot
->ocs
);
291 pnv_php_reverse_nodes(php_slot
->dn
);
292 ret
= pnv_php_populate_changeset(&php_slot
->ocs
, php_slot
->dn
);
294 pnv_php_reverse_nodes(php_slot
->dn
);
295 SLOT_WARN(php_slot
, "Error %d populating changeset\n",
300 php_slot
->dn
->child
= NULL
;
301 ret
= of_changeset_apply(&php_slot
->ocs
);
303 SLOT_WARN(php_slot
, "Error %d applying changeset\n", ret
);
304 goto destroy_changeset
;
307 /* Add device node firmware data */
308 pnv_php_add_pdns(php_slot
);
315 of_changeset_destroy(&php_slot
->ocs
);
318 php_slot
->dn
->child
= NULL
;
327 static inline struct pnv_php_slot
*to_pnv_php_slot(struct hotplug_slot
*slot
)
329 return container_of(slot
, struct pnv_php_slot
, slot
);
332 int pnv_php_set_slot_power_state(struct hotplug_slot
*slot
,
335 struct pnv_php_slot
*php_slot
= to_pnv_php_slot(slot
);
339 ret
= pnv_pci_set_power_state(php_slot
->id
, state
, &msg
);
341 if (be64_to_cpu(msg
.params
[1]) != php_slot
->dn
->phandle
||
342 be64_to_cpu(msg
.params
[2]) != state
) {
343 SLOT_WARN(php_slot
, "Wrong msg (%lld, %lld, %lld)\n",
344 be64_to_cpu(msg
.params
[1]),
345 be64_to_cpu(msg
.params
[2]),
346 be64_to_cpu(msg
.params
[3]));
349 if (be64_to_cpu(msg
.params
[3]) != OPAL_SUCCESS
) {
353 } else if (ret
< 0) {
357 if (state
== OPAL_PCI_SLOT_POWER_OFF
|| state
== OPAL_PCI_SLOT_OFFLINE
)
358 pnv_php_rmv_devtree(php_slot
);
360 ret
= pnv_php_add_devtree(php_slot
);
365 SLOT_WARN(php_slot
, "Error %d powering %s\n",
366 ret
, (state
== OPAL_PCI_SLOT_POWER_ON
) ? "on" : "off");
369 EXPORT_SYMBOL_GPL(pnv_php_set_slot_power_state
);
371 static int pnv_php_get_power_state(struct hotplug_slot
*slot
, u8
*state
)
373 struct pnv_php_slot
*php_slot
= to_pnv_php_slot(slot
);
374 uint8_t power_state
= OPAL_PCI_SLOT_POWER_ON
;
378 * Retrieve power status from firmware. If we fail
379 * getting that, the power status fails back to
382 ret
= pnv_pci_get_power_state(php_slot
->id
, &power_state
);
384 SLOT_WARN(php_slot
, "Error %d getting power status\n",
387 *state
= power_state
;
393 static int pnv_php_get_adapter_state(struct hotplug_slot
*slot
, u8
*state
)
395 struct pnv_php_slot
*php_slot
= to_pnv_php_slot(slot
);
396 uint8_t presence
= OPAL_PCI_SLOT_EMPTY
;
400 * Retrieve presence status from firmware. If we can't
401 * get that, it will fail back to be empty.
403 ret
= pnv_pci_get_presence_state(php_slot
->id
, &presence
);
408 SLOT_WARN(php_slot
, "Error %d getting presence\n", ret
);
414 static int pnv_php_get_attention_state(struct hotplug_slot
*slot
, u8
*state
)
416 struct pnv_php_slot
*php_slot
= to_pnv_php_slot(slot
);
418 *state
= php_slot
->attention_state
;
422 static int pnv_php_set_attention_state(struct hotplug_slot
*slot
, u8 state
)
424 struct pnv_php_slot
*php_slot
= to_pnv_php_slot(slot
);
425 struct pci_dev
*bridge
= php_slot
->pdev
;
428 php_slot
->attention_state
= state
;
432 mask
= PCI_EXP_SLTCTL_AIC
;
435 new = PCI_EXP_SLTCTL_ATTN_IND_ON
;
437 new = PCI_EXP_SLTCTL_ATTN_IND_OFF
;
439 pcie_capability_clear_and_set_word(bridge
, PCI_EXP_SLTCTL
, mask
, new);
444 static int pnv_php_enable(struct pnv_php_slot
*php_slot
, bool rescan
)
446 struct hotplug_slot
*slot
= &php_slot
->slot
;
447 uint8_t presence
= OPAL_PCI_SLOT_EMPTY
;
448 uint8_t power_status
= OPAL_PCI_SLOT_POWER_ON
;
451 /* Check if the slot has been configured */
452 if (php_slot
->state
!= PNV_PHP_STATE_REGISTERED
)
455 /* Retrieve slot presence status */
456 ret
= pnv_php_get_adapter_state(slot
, &presence
);
461 * Proceed if there have nothing behind the slot. However,
462 * we should leave the slot in registered state at the
463 * beginning. Otherwise, the PCI devices inserted afterwards
464 * won't be probed and populated.
466 if (presence
== OPAL_PCI_SLOT_EMPTY
) {
467 if (!php_slot
->power_state_check
) {
468 php_slot
->power_state_check
= true;
477 * If the power supply to the slot is off, we can't detect
478 * adapter presence state. That means we have to turn the
479 * slot on before going to probe slot's presence state.
481 * On the first time, we don't change the power status to
482 * boost system boot with assumption that the firmware
483 * supplies consistent slot power status: empty slot always
484 * has its power off and non-empty slot has its power on.
486 if (!php_slot
->power_state_check
) {
487 php_slot
->power_state_check
= true;
489 ret
= pnv_php_get_power_state(slot
, &power_status
);
493 if (power_status
!= OPAL_PCI_SLOT_POWER_ON
)
497 /* Check the power status. Scan the slot if it is already on */
498 ret
= pnv_php_get_power_state(slot
, &power_status
);
502 if (power_status
== OPAL_PCI_SLOT_POWER_ON
)
505 /* Power is off, turn it on and then scan the slot */
506 ret
= pnv_php_set_slot_power_state(slot
, OPAL_PCI_SLOT_POWER_ON
);
511 if (presence
== OPAL_PCI_SLOT_PRESENT
) {
513 pci_lock_rescan_remove();
514 pci_hp_add_devices(php_slot
->bus
);
515 pci_unlock_rescan_remove();
518 /* Rescan for child hotpluggable slots */
519 php_slot
->state
= PNV_PHP_STATE_POPULATED
;
521 pnv_php_register(php_slot
->dn
);
523 php_slot
->state
= PNV_PHP_STATE_POPULATED
;
529 static int pnv_php_reset_slot(struct hotplug_slot
*slot
, int probe
)
531 struct pnv_php_slot
*php_slot
= to_pnv_php_slot(slot
);
532 struct pci_dev
*bridge
= php_slot
->pdev
;
536 * The CAPI folks want pnv_php to drive OpenCAPI slots
537 * which don't have a bridge. Only claim to support
538 * reset_slot() if we have a bridge device (for now...)
543 /* mask our interrupt while resetting the bridge */
544 if (php_slot
->irq
> 0)
545 disable_irq(php_slot
->irq
);
547 pci_bridge_secondary_bus_reset(bridge
);
549 /* clear any state changes that happened due to the reset */
550 pcie_capability_read_word(php_slot
->pdev
, PCI_EXP_SLTSTA
, &sts
);
551 sts
&= (PCI_EXP_SLTSTA_PDC
| PCI_EXP_SLTSTA_DLLSC
);
552 pcie_capability_write_word(php_slot
->pdev
, PCI_EXP_SLTSTA
, sts
);
554 if (php_slot
->irq
> 0)
555 enable_irq(php_slot
->irq
);
560 static int pnv_php_enable_slot(struct hotplug_slot
*slot
)
562 struct pnv_php_slot
*php_slot
= to_pnv_php_slot(slot
);
564 return pnv_php_enable(php_slot
, true);
567 static int pnv_php_disable_slot(struct hotplug_slot
*slot
)
569 struct pnv_php_slot
*php_slot
= to_pnv_php_slot(slot
);
573 * Allow to disable a slot already in the registered state to
574 * cover cases where the slot couldn't be enabled and never
575 * reached the populated state
577 if (php_slot
->state
!= PNV_PHP_STATE_POPULATED
&&
578 php_slot
->state
!= PNV_PHP_STATE_REGISTERED
)
581 /* Remove all devices behind the slot */
582 pci_lock_rescan_remove();
583 pci_hp_remove_devices(php_slot
->bus
);
584 pci_unlock_rescan_remove();
586 /* Detach the child hotpluggable slots */
587 pnv_php_unregister(php_slot
->dn
);
589 /* Notify firmware and remove device nodes */
590 ret
= pnv_php_set_slot_power_state(slot
, OPAL_PCI_SLOT_POWER_OFF
);
592 php_slot
->state
= PNV_PHP_STATE_REGISTERED
;
596 static const struct hotplug_slot_ops php_slot_ops
= {
597 .get_power_status
= pnv_php_get_power_state
,
598 .get_adapter_status
= pnv_php_get_adapter_state
,
599 .get_attention_status
= pnv_php_get_attention_state
,
600 .set_attention_status
= pnv_php_set_attention_state
,
601 .enable_slot
= pnv_php_enable_slot
,
602 .disable_slot
= pnv_php_disable_slot
,
603 .reset_slot
= pnv_php_reset_slot
,
606 static void pnv_php_release(struct pnv_php_slot
*php_slot
)
610 /* Remove from global or child list */
611 spin_lock_irqsave(&pnv_php_lock
, flags
);
612 list_del(&php_slot
->link
);
613 spin_unlock_irqrestore(&pnv_php_lock
, flags
);
615 /* Detach from parent */
616 pnv_php_put_slot(php_slot
);
617 pnv_php_put_slot(php_slot
->parent
);
620 static struct pnv_php_slot
*pnv_php_alloc_slot(struct device_node
*dn
)
622 struct pnv_php_slot
*php_slot
;
628 ret
= of_property_read_string(dn
, "ibm,slot-label", &label
);
632 if (pnv_pci_get_slot_id(dn
, &id
))
635 bus
= pci_find_bus_by_node(dn
);
639 php_slot
= kzalloc(sizeof(*php_slot
), GFP_KERNEL
);
643 php_slot
->name
= kstrdup(label
, GFP_KERNEL
);
644 if (!php_slot
->name
) {
649 if (dn
->child
&& PCI_DN(dn
->child
))
650 php_slot
->slot_no
= PCI_SLOT(PCI_DN(dn
->child
)->devfn
);
652 php_slot
->slot_no
= -1; /* Placeholder slot */
654 kref_init(&php_slot
->kref
);
655 php_slot
->state
= PNV_PHP_STATE_INITIALIZED
;
657 php_slot
->pdev
= bus
->self
;
660 php_slot
->power_state_check
= false;
661 php_slot
->slot
.ops
= &php_slot_ops
;
663 INIT_LIST_HEAD(&php_slot
->children
);
664 INIT_LIST_HEAD(&php_slot
->link
);
669 static int pnv_php_register_slot(struct pnv_php_slot
*php_slot
)
671 struct pnv_php_slot
*parent
;
672 struct device_node
*dn
= php_slot
->dn
;
676 /* Check if the slot is registered or not */
677 parent
= pnv_php_find_slot(php_slot
->dn
);
679 pnv_php_put_slot(parent
);
683 /* Register PCI slot */
684 ret
= pci_hp_register(&php_slot
->slot
, php_slot
->bus
,
685 php_slot
->slot_no
, php_slot
->name
);
687 SLOT_WARN(php_slot
, "Error %d registering slot\n", ret
);
691 /* Attach to the parent's child list or global list */
692 while ((dn
= of_get_parent(dn
))) {
698 parent
= pnv_php_find_slot(dn
);
707 spin_lock_irqsave(&pnv_php_lock
, flags
);
708 php_slot
->parent
= parent
;
710 list_add_tail(&php_slot
->link
, &parent
->children
);
712 list_add_tail(&php_slot
->link
, &pnv_php_slot_list
);
713 spin_unlock_irqrestore(&pnv_php_lock
, flags
);
715 php_slot
->state
= PNV_PHP_STATE_REGISTERED
;
719 static int pnv_php_enable_msix(struct pnv_php_slot
*php_slot
)
721 struct pci_dev
*pdev
= php_slot
->pdev
;
722 struct msix_entry entry
;
726 /* Get total number of MSIx entries */
727 nr_entries
= pci_msix_vec_count(pdev
);
731 /* Check hotplug MSIx entry is in range */
732 pcie_capability_read_word(pdev
, PCI_EXP_FLAGS
, &pcie_flag
);
733 entry
.entry
= (pcie_flag
& PCI_EXP_FLAGS_IRQ
) >> 9;
734 if (entry
.entry
>= nr_entries
)
738 ret
= pci_enable_msix_exact(pdev
, &entry
, 1);
740 SLOT_WARN(php_slot
, "Error %d enabling MSIx\n", ret
);
747 static void pnv_php_event_handler(struct work_struct
*work
)
749 struct pnv_php_event
*event
=
750 container_of(work
, struct pnv_php_event
, work
);
751 struct pnv_php_slot
*php_slot
= event
->php_slot
;
754 pnv_php_enable_slot(&php_slot
->slot
);
756 pnv_php_disable_slot(&php_slot
->slot
);
761 static irqreturn_t
pnv_php_interrupt(int irq
, void *data
)
763 struct pnv_php_slot
*php_slot
= data
;
764 struct pci_dev
*pchild
, *pdev
= php_slot
->pdev
;
765 struct eeh_dev
*edev
;
767 struct pnv_php_event
*event
;
774 pcie_capability_read_word(pdev
, PCI_EXP_SLTSTA
, &sts
);
775 sts
&= (PCI_EXP_SLTSTA_PDC
| PCI_EXP_SLTSTA_DLLSC
);
776 pcie_capability_write_word(pdev
, PCI_EXP_SLTSTA
, sts
);
778 pci_dbg(pdev
, "PCI slot [%s]: HP int! DLAct: %d, PresDet: %d\n",
780 !!(sts
& PCI_EXP_SLTSTA_DLLSC
),
781 !!(sts
& PCI_EXP_SLTSTA_PDC
));
783 if (sts
& PCI_EXP_SLTSTA_DLLSC
) {
784 pcie_capability_read_word(pdev
, PCI_EXP_LNKSTA
, &lsts
);
785 added
= !!(lsts
& PCI_EXP_LNKSTA_DLLLA
);
786 } else if (!(php_slot
->flags
& PNV_PHP_FLAG_BROKEN_PDC
) &&
787 (sts
& PCI_EXP_SLTSTA_PDC
)) {
788 ret
= pnv_pci_get_presence_state(php_slot
->id
, &presence
);
791 "PCI slot [%s] error %d getting presence (0x%04x), to retry the operation.\n",
792 php_slot
->name
, ret
, sts
);
796 added
= !!(presence
== OPAL_PCI_SLOT_PRESENT
);
798 pci_dbg(pdev
, "PCI slot [%s]: Spurious IRQ?\n", php_slot
->name
);
802 /* Freeze the removed PE to avoid unexpected error reporting */
804 pchild
= list_first_entry_or_null(&php_slot
->bus
->devices
,
805 struct pci_dev
, bus_list
);
806 edev
= pchild
? pci_dev_to_eeh_dev(pchild
) : NULL
;
807 pe
= edev
? edev
->pe
: NULL
;
809 eeh_serialize_lock(&flags
);
810 eeh_pe_mark_isolated(pe
);
811 eeh_serialize_unlock(flags
);
812 eeh_pe_set_option(pe
, EEH_OPT_FREEZE_PE
);
817 * The PE is left in frozen state if the event is missed. It's
818 * fine as the PCI devices (PE) aren't functional any more.
820 event
= kzalloc(sizeof(*event
), GFP_ATOMIC
);
823 "PCI slot [%s] missed hotplug event 0x%04x\n",
824 php_slot
->name
, sts
);
828 pci_info(pdev
, "PCI slot [%s] %s (IRQ: %d)\n",
829 php_slot
->name
, added
? "added" : "removed", irq
);
830 INIT_WORK(&event
->work
, pnv_php_event_handler
);
831 event
->added
= added
;
832 event
->php_slot
= php_slot
;
833 queue_work(php_slot
->wq
, &event
->work
);
838 static void pnv_php_init_irq(struct pnv_php_slot
*php_slot
, int irq
)
840 struct pci_dev
*pdev
= php_slot
->pdev
;
845 /* Allocate workqueue */
846 php_slot
->wq
= alloc_workqueue("pciehp-%s", 0, 0, php_slot
->name
);
848 SLOT_WARN(php_slot
, "Cannot alloc workqueue\n");
849 pnv_php_disable_irq(php_slot
, true);
853 /* Check PDC (Presence Detection Change) is broken or not */
854 ret
= of_property_read_u32(php_slot
->dn
, "ibm,slot-broken-pdc",
856 if (!ret
&& broken_pdc
)
857 php_slot
->flags
|= PNV_PHP_FLAG_BROKEN_PDC
;
859 /* Clear pending interrupts */
860 pcie_capability_read_word(pdev
, PCI_EXP_SLTSTA
, &sts
);
861 if (php_slot
->flags
& PNV_PHP_FLAG_BROKEN_PDC
)
862 sts
|= PCI_EXP_SLTSTA_DLLSC
;
864 sts
|= (PCI_EXP_SLTSTA_PDC
| PCI_EXP_SLTSTA_DLLSC
);
865 pcie_capability_write_word(pdev
, PCI_EXP_SLTSTA
, sts
);
867 /* Request the interrupt */
868 ret
= request_irq(irq
, pnv_php_interrupt
, IRQF_SHARED
,
869 php_slot
->name
, php_slot
);
871 pnv_php_disable_irq(php_slot
, true);
872 SLOT_WARN(php_slot
, "Error %d enabling IRQ %d\n", ret
, irq
);
876 /* Enable the interrupts */
877 pcie_capability_read_word(pdev
, PCI_EXP_SLTCTL
, &ctrl
);
878 if (php_slot
->flags
& PNV_PHP_FLAG_BROKEN_PDC
) {
879 ctrl
&= ~PCI_EXP_SLTCTL_PDCE
;
880 ctrl
|= (PCI_EXP_SLTCTL_HPIE
|
881 PCI_EXP_SLTCTL_DLLSCE
);
883 ctrl
|= (PCI_EXP_SLTCTL_HPIE
|
884 PCI_EXP_SLTCTL_PDCE
|
885 PCI_EXP_SLTCTL_DLLSCE
);
887 pcie_capability_write_word(pdev
, PCI_EXP_SLTCTL
, ctrl
);
889 /* The interrupt is initialized successfully when @irq is valid */
893 static void pnv_php_enable_irq(struct pnv_php_slot
*php_slot
)
895 struct pci_dev
*pdev
= php_slot
->pdev
;
899 * The MSI/MSIx interrupt might have been occupied by other
900 * drivers. Don't populate the surprise hotplug capability
903 if (pci_dev_msi_enabled(pdev
))
906 ret
= pci_enable_device(pdev
);
908 SLOT_WARN(php_slot
, "Error %d enabling device\n", ret
);
912 pci_set_master(pdev
);
914 /* Enable MSIx interrupt */
915 irq
= pnv_php_enable_msix(php_slot
);
917 pnv_php_init_irq(php_slot
, irq
);
922 * Use MSI if MSIx doesn't work. Fail back to legacy INTx
923 * if MSI doesn't work either
925 ret
= pci_enable_msi(pdev
);
926 if (!ret
|| pdev
->irq
) {
928 pnv_php_init_irq(php_slot
, irq
);
932 static int pnv_php_register_one(struct device_node
*dn
)
934 struct pnv_php_slot
*php_slot
;
938 /* Check if it's hotpluggable slot */
939 ret
= of_property_read_u32(dn
, "ibm,slot-pluggable", &prop32
);
943 ret
= of_property_read_u32(dn
, "ibm,reset-by-firmware", &prop32
);
947 php_slot
= pnv_php_alloc_slot(dn
);
951 ret
= pnv_php_register_slot(php_slot
);
955 ret
= pnv_php_enable(php_slot
, false);
957 goto unregister_slot
;
959 /* Enable interrupt if the slot supports surprise hotplug */
960 ret
= of_property_read_u32(dn
, "ibm,slot-surprise-pluggable", &prop32
);
962 pnv_php_enable_irq(php_slot
);
967 pnv_php_unregister_one(php_slot
->dn
);
969 pnv_php_put_slot(php_slot
);
973 static void pnv_php_register(struct device_node
*dn
)
975 struct device_node
*child
;
978 * The parent slots should be registered before their
981 for_each_child_of_node(dn
, child
) {
982 pnv_php_register_one(child
);
983 pnv_php_register(child
);
987 static void pnv_php_unregister_one(struct device_node
*dn
)
989 struct pnv_php_slot
*php_slot
;
991 php_slot
= pnv_php_find_slot(dn
);
995 php_slot
->state
= PNV_PHP_STATE_OFFLINE
;
996 pci_hp_deregister(&php_slot
->slot
);
997 pnv_php_release(php_slot
);
998 pnv_php_put_slot(php_slot
);
1001 static void pnv_php_unregister(struct device_node
*dn
)
1003 struct device_node
*child
;
1005 /* The child slots should go before their parent slots */
1006 for_each_child_of_node(dn
, child
) {
1007 pnv_php_unregister(child
);
1008 pnv_php_unregister_one(child
);
1012 static int __init
pnv_php_init(void)
1014 struct device_node
*dn
;
1016 pr_info(DRIVER_DESC
" version: " DRIVER_VERSION
"\n");
1017 for_each_compatible_node(dn
, NULL
, "ibm,ioda2-phb")
1018 pnv_php_register(dn
);
1020 for_each_compatible_node(dn
, NULL
, "ibm,ioda3-phb")
1021 pnv_php_register(dn
);
1023 for_each_compatible_node(dn
, NULL
, "ibm,ioda2-npu2-opencapi-phb")
1024 pnv_php_register_one(dn
); /* slot directly under the PHB */
1028 static void __exit
pnv_php_exit(void)
1030 struct device_node
*dn
;
1032 for_each_compatible_node(dn
, NULL
, "ibm,ioda2-phb")
1033 pnv_php_unregister(dn
);
1035 for_each_compatible_node(dn
, NULL
, "ibm,ioda3-phb")
1036 pnv_php_unregister(dn
);
1038 for_each_compatible_node(dn
, NULL
, "ibm,ioda2-npu2-opencapi-phb")
1039 pnv_php_unregister_one(dn
); /* slot directly under the PHB */
1042 module_init(pnv_php_init
);
1043 module_exit(pnv_php_exit
);
1045 MODULE_VERSION(DRIVER_VERSION
);
1046 MODULE_LICENSE("GPL v2");
1047 MODULE_AUTHOR(DRIVER_AUTHOR
);
1048 MODULE_DESCRIPTION(DRIVER_DESC
);