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
;
45 if (php_slot
->irq
> 0) {
46 pcie_capability_read_word(pdev
, PCI_EXP_SLTCTL
, &ctrl
);
47 ctrl
&= ~(PCI_EXP_SLTCTL_HPIE
|
49 PCI_EXP_SLTCTL_DLLSCE
);
50 pcie_capability_write_word(pdev
, PCI_EXP_SLTCTL
, ctrl
);
52 free_irq(php_slot
->irq
, php_slot
);
57 destroy_workqueue(php_slot
->wq
);
62 if (pdev
->msix_enabled
)
63 pci_disable_msix(pdev
);
64 else if (pdev
->msi_enabled
)
65 pci_disable_msi(pdev
);
67 pci_disable_device(pdev
);
71 static void pnv_php_free_slot(struct kref
*kref
)
73 struct pnv_php_slot
*php_slot
= container_of(kref
,
74 struct pnv_php_slot
, kref
);
76 WARN_ON(!list_empty(&php_slot
->children
));
77 pnv_php_disable_irq(php_slot
, false);
78 kfree(php_slot
->name
);
82 static inline void pnv_php_put_slot(struct pnv_php_slot
*php_slot
)
88 kref_put(&php_slot
->kref
, pnv_php_free_slot
);
91 static struct pnv_php_slot
*pnv_php_match(struct device_node
*dn
,
92 struct pnv_php_slot
*php_slot
)
94 struct pnv_php_slot
*target
, *tmp
;
96 if (php_slot
->dn
== dn
) {
97 kref_get(&php_slot
->kref
);
101 list_for_each_entry(tmp
, &php_slot
->children
, link
) {
102 target
= pnv_php_match(dn
, tmp
);
110 struct pnv_php_slot
*pnv_php_find_slot(struct device_node
*dn
)
112 struct pnv_php_slot
*php_slot
, *tmp
;
115 spin_lock_irqsave(&pnv_php_lock
, flags
);
116 list_for_each_entry(tmp
, &pnv_php_slot_list
, link
) {
117 php_slot
= pnv_php_match(dn
, tmp
);
119 spin_unlock_irqrestore(&pnv_php_lock
, flags
);
123 spin_unlock_irqrestore(&pnv_php_lock
, flags
);
127 EXPORT_SYMBOL_GPL(pnv_php_find_slot
);
130 * Remove pdn for all children of the indicated device node.
131 * The function should remove pdn in a depth-first manner.
133 static void pnv_php_rmv_pdns(struct device_node
*dn
)
135 struct device_node
*child
;
137 for_each_child_of_node(dn
, child
) {
138 pnv_php_rmv_pdns(child
);
140 pci_remove_device_node_info(child
);
145 * Detach all child nodes of the indicated device nodes. The
146 * function should handle device nodes in depth-first manner.
148 * We should not invoke of_node_release() as the memory for
149 * individual device node is part of large memory block. The
150 * large block is allocated from memblock (system bootup) or
151 * kmalloc() when unflattening the device tree by OF changeset.
152 * We can not free the large block allocated from memblock. For
153 * later case, it should be released at once.
155 static void pnv_php_detach_device_nodes(struct device_node
*parent
)
157 struct device_node
*dn
;
159 for_each_child_of_node(parent
, dn
) {
160 pnv_php_detach_device_nodes(dn
);
167 static void pnv_php_rmv_devtree(struct pnv_php_slot
*php_slot
)
169 pnv_php_rmv_pdns(php_slot
->dn
);
172 * Decrease the refcount if the device nodes were created
173 * through OF changeset before detaching them.
176 of_changeset_destroy(&php_slot
->ocs
);
177 pnv_php_detach_device_nodes(php_slot
->dn
);
181 kfree(php_slot
->fdt
);
183 php_slot
->dn
->child
= NULL
;
184 php_slot
->fdt
= NULL
;
189 * As the nodes in OF changeset are applied in reverse order, we
190 * need revert the nodes in advance so that we have correct node
191 * order after the changeset is applied.
193 static void pnv_php_reverse_nodes(struct device_node
*parent
)
195 struct device_node
*child
, *next
;
198 for_each_child_of_node(parent
, child
)
199 pnv_php_reverse_nodes(child
);
201 /* Reverse the nodes in the child list */
202 child
= parent
->child
;
203 parent
->child
= NULL
;
205 next
= child
->sibling
;
207 child
->sibling
= parent
->child
;
208 parent
->child
= child
;
213 static int pnv_php_populate_changeset(struct of_changeset
*ocs
,
214 struct device_node
*dn
)
216 struct device_node
*child
;
219 for_each_child_of_node(dn
, child
) {
220 ret
= of_changeset_attach_node(ocs
, child
);
226 ret
= pnv_php_populate_changeset(ocs
, child
);
236 static void *pnv_php_add_one_pdn(struct device_node
*dn
, void *data
)
238 struct pci_controller
*hose
= (struct pci_controller
*)data
;
241 pdn
= pci_add_device_node_info(hose
, dn
);
243 return ERR_PTR(-ENOMEM
);
248 static void pnv_php_add_pdns(struct pnv_php_slot
*slot
)
250 struct pci_controller
*hose
= pci_bus_to_host(slot
->bus
);
252 pci_traverse_device_nodes(slot
->dn
, pnv_php_add_one_pdn
, hose
);
255 static int pnv_php_add_devtree(struct pnv_php_slot
*php_slot
)
257 void *fdt
, *fdt1
, *dt
;
260 /* We don't know the FDT blob size. We try to get it through
261 * maximal memory chunk and then copy it to another chunk that
262 * fits the real size.
264 fdt1
= kzalloc(0x10000, GFP_KERNEL
);
270 ret
= pnv_pci_get_device_tree(php_slot
->dn
->phandle
, fdt1
, 0x10000);
272 SLOT_WARN(php_slot
, "Error %d getting FDT blob\n", ret
);
276 fdt
= kmemdup(fdt1
, fdt_totalsize(fdt1
), GFP_KERNEL
);
282 /* Unflatten device tree blob */
283 dt
= of_fdt_unflatten_tree(fdt
, php_slot
->dn
, NULL
);
286 SLOT_WARN(php_slot
, "Cannot unflatten FDT\n");
290 /* Initialize and apply the changeset */
291 of_changeset_init(&php_slot
->ocs
);
292 pnv_php_reverse_nodes(php_slot
->dn
);
293 ret
= pnv_php_populate_changeset(&php_slot
->ocs
, php_slot
->dn
);
295 pnv_php_reverse_nodes(php_slot
->dn
);
296 SLOT_WARN(php_slot
, "Error %d populating changeset\n",
301 php_slot
->dn
->child
= NULL
;
302 ret
= of_changeset_apply(&php_slot
->ocs
);
304 SLOT_WARN(php_slot
, "Error %d applying changeset\n", ret
);
305 goto destroy_changeset
;
308 /* Add device node firmware data */
309 pnv_php_add_pdns(php_slot
);
316 of_changeset_destroy(&php_slot
->ocs
);
319 php_slot
->dn
->child
= NULL
;
328 static inline struct pnv_php_slot
*to_pnv_php_slot(struct hotplug_slot
*slot
)
330 return container_of(slot
, struct pnv_php_slot
, slot
);
333 int pnv_php_set_slot_power_state(struct hotplug_slot
*slot
,
336 struct pnv_php_slot
*php_slot
= to_pnv_php_slot(slot
);
340 ret
= pnv_pci_set_power_state(php_slot
->id
, state
, &msg
);
342 if (be64_to_cpu(msg
.params
[1]) != php_slot
->dn
->phandle
||
343 be64_to_cpu(msg
.params
[2]) != state
) {
344 SLOT_WARN(php_slot
, "Wrong msg (%lld, %lld, %lld)\n",
345 be64_to_cpu(msg
.params
[1]),
346 be64_to_cpu(msg
.params
[2]),
347 be64_to_cpu(msg
.params
[3]));
350 if (be64_to_cpu(msg
.params
[3]) != OPAL_SUCCESS
) {
354 } else if (ret
< 0) {
358 if (state
== OPAL_PCI_SLOT_POWER_OFF
|| state
== OPAL_PCI_SLOT_OFFLINE
)
359 pnv_php_rmv_devtree(php_slot
);
361 ret
= pnv_php_add_devtree(php_slot
);
366 SLOT_WARN(php_slot
, "Error %d powering %s\n",
367 ret
, (state
== OPAL_PCI_SLOT_POWER_ON
) ? "on" : "off");
370 EXPORT_SYMBOL_GPL(pnv_php_set_slot_power_state
);
372 static int pnv_php_get_power_state(struct hotplug_slot
*slot
, u8
*state
)
374 struct pnv_php_slot
*php_slot
= to_pnv_php_slot(slot
);
375 uint8_t power_state
= OPAL_PCI_SLOT_POWER_ON
;
379 * Retrieve power status from firmware. If we fail
380 * getting that, the power status fails back to
383 ret
= pnv_pci_get_power_state(php_slot
->id
, &power_state
);
385 SLOT_WARN(php_slot
, "Error %d getting power status\n",
388 *state
= power_state
;
394 static int pnv_php_get_adapter_state(struct hotplug_slot
*slot
, u8
*state
)
396 struct pnv_php_slot
*php_slot
= to_pnv_php_slot(slot
);
397 uint8_t presence
= OPAL_PCI_SLOT_EMPTY
;
401 * Retrieve presence status from firmware. If we can't
402 * get that, it will fail back to be empty.
404 ret
= pnv_pci_get_presence_state(php_slot
->id
, &presence
);
409 SLOT_WARN(php_slot
, "Error %d getting presence\n", ret
);
415 static int pnv_php_get_attention_state(struct hotplug_slot
*slot
, u8
*state
)
417 struct pnv_php_slot
*php_slot
= to_pnv_php_slot(slot
);
419 *state
= php_slot
->attention_state
;
423 static int pnv_php_set_attention_state(struct hotplug_slot
*slot
, u8 state
)
425 struct pnv_php_slot
*php_slot
= to_pnv_php_slot(slot
);
426 struct pci_dev
*bridge
= php_slot
->pdev
;
429 php_slot
->attention_state
= state
;
433 mask
= PCI_EXP_SLTCTL_AIC
;
436 new = PCI_EXP_SLTCTL_ATTN_IND_ON
;
438 new = PCI_EXP_SLTCTL_ATTN_IND_OFF
;
440 pcie_capability_clear_and_set_word(bridge
, PCI_EXP_SLTCTL
, mask
, new);
445 static int pnv_php_enable(struct pnv_php_slot
*php_slot
, bool rescan
)
447 struct hotplug_slot
*slot
= &php_slot
->slot
;
448 uint8_t presence
= OPAL_PCI_SLOT_EMPTY
;
449 uint8_t power_status
= OPAL_PCI_SLOT_POWER_ON
;
452 /* Check if the slot has been configured */
453 if (php_slot
->state
!= PNV_PHP_STATE_REGISTERED
)
456 /* Retrieve slot presence status */
457 ret
= pnv_php_get_adapter_state(slot
, &presence
);
462 * Proceed if there have nothing behind the slot. However,
463 * we should leave the slot in registered state at the
464 * beginning. Otherwise, the PCI devices inserted afterwards
465 * won't be probed and populated.
467 if (presence
== OPAL_PCI_SLOT_EMPTY
) {
468 if (!php_slot
->power_state_check
) {
469 php_slot
->power_state_check
= true;
478 * If the power supply to the slot is off, we can't detect
479 * adapter presence state. That means we have to turn the
480 * slot on before going to probe slot's presence state.
482 * On the first time, we don't change the power status to
483 * boost system boot with assumption that the firmware
484 * supplies consistent slot power status: empty slot always
485 * has its power off and non-empty slot has its power on.
487 if (!php_slot
->power_state_check
) {
488 php_slot
->power_state_check
= true;
490 ret
= pnv_php_get_power_state(slot
, &power_status
);
494 if (power_status
!= OPAL_PCI_SLOT_POWER_ON
)
498 /* Check the power status. Scan the slot if it is already on */
499 ret
= pnv_php_get_power_state(slot
, &power_status
);
503 if (power_status
== OPAL_PCI_SLOT_POWER_ON
)
506 /* Power is off, turn it on and then scan the slot */
507 ret
= pnv_php_set_slot_power_state(slot
, OPAL_PCI_SLOT_POWER_ON
);
512 if (presence
== OPAL_PCI_SLOT_PRESENT
) {
514 pci_lock_rescan_remove();
515 pci_hp_add_devices(php_slot
->bus
);
516 pci_unlock_rescan_remove();
519 /* Rescan for child hotpluggable slots */
520 php_slot
->state
= PNV_PHP_STATE_POPULATED
;
522 pnv_php_register(php_slot
->dn
);
524 php_slot
->state
= PNV_PHP_STATE_POPULATED
;
530 static int pnv_php_reset_slot(struct hotplug_slot
*slot
, bool probe
)
532 struct pnv_php_slot
*php_slot
= to_pnv_php_slot(slot
);
533 struct pci_dev
*bridge
= php_slot
->pdev
;
537 * The CAPI folks want pnv_php to drive OpenCAPI slots
538 * which don't have a bridge. Only claim to support
539 * reset_slot() if we have a bridge device (for now...)
544 /* mask our interrupt while resetting the bridge */
545 if (php_slot
->irq
> 0)
546 disable_irq(php_slot
->irq
);
548 pci_bridge_secondary_bus_reset(bridge
);
550 /* clear any state changes that happened due to the reset */
551 pcie_capability_read_word(php_slot
->pdev
, PCI_EXP_SLTSTA
, &sts
);
552 sts
&= (PCI_EXP_SLTSTA_PDC
| PCI_EXP_SLTSTA_DLLSC
);
553 pcie_capability_write_word(php_slot
->pdev
, PCI_EXP_SLTSTA
, sts
);
555 if (php_slot
->irq
> 0)
556 enable_irq(php_slot
->irq
);
561 static int pnv_php_enable_slot(struct hotplug_slot
*slot
)
563 struct pnv_php_slot
*php_slot
= to_pnv_php_slot(slot
);
565 return pnv_php_enable(php_slot
, true);
568 static int pnv_php_disable_slot(struct hotplug_slot
*slot
)
570 struct pnv_php_slot
*php_slot
= to_pnv_php_slot(slot
);
574 * Allow to disable a slot already in the registered state to
575 * cover cases where the slot couldn't be enabled and never
576 * reached the populated state
578 if (php_slot
->state
!= PNV_PHP_STATE_POPULATED
&&
579 php_slot
->state
!= PNV_PHP_STATE_REGISTERED
)
582 /* Remove all devices behind the slot */
583 pci_lock_rescan_remove();
584 pci_hp_remove_devices(php_slot
->bus
);
585 pci_unlock_rescan_remove();
587 /* Detach the child hotpluggable slots */
588 pnv_php_unregister(php_slot
->dn
);
590 /* Notify firmware and remove device nodes */
591 ret
= pnv_php_set_slot_power_state(slot
, OPAL_PCI_SLOT_POWER_OFF
);
593 php_slot
->state
= PNV_PHP_STATE_REGISTERED
;
597 static const struct hotplug_slot_ops php_slot_ops
= {
598 .get_power_status
= pnv_php_get_power_state
,
599 .get_adapter_status
= pnv_php_get_adapter_state
,
600 .get_attention_status
= pnv_php_get_attention_state
,
601 .set_attention_status
= pnv_php_set_attention_state
,
602 .enable_slot
= pnv_php_enable_slot
,
603 .disable_slot
= pnv_php_disable_slot
,
604 .reset_slot
= pnv_php_reset_slot
,
607 static void pnv_php_release(struct pnv_php_slot
*php_slot
)
611 /* Remove from global or child list */
612 spin_lock_irqsave(&pnv_php_lock
, flags
);
613 list_del(&php_slot
->link
);
614 spin_unlock_irqrestore(&pnv_php_lock
, flags
);
616 /* Detach from parent */
617 pnv_php_put_slot(php_slot
);
618 pnv_php_put_slot(php_slot
->parent
);
621 static struct pnv_php_slot
*pnv_php_alloc_slot(struct device_node
*dn
)
623 struct pnv_php_slot
*php_slot
;
629 ret
= of_property_read_string(dn
, "ibm,slot-label", &label
);
633 if (pnv_pci_get_slot_id(dn
, &id
))
636 bus
= pci_find_bus_by_node(dn
);
640 php_slot
= kzalloc(sizeof(*php_slot
), GFP_KERNEL
);
644 php_slot
->name
= kstrdup(label
, GFP_KERNEL
);
645 if (!php_slot
->name
) {
650 if (dn
->child
&& PCI_DN(dn
->child
))
651 php_slot
->slot_no
= PCI_SLOT(PCI_DN(dn
->child
)->devfn
);
653 php_slot
->slot_no
= -1; /* Placeholder slot */
655 kref_init(&php_slot
->kref
);
656 php_slot
->state
= PNV_PHP_STATE_INITIALIZED
;
658 php_slot
->pdev
= bus
->self
;
661 php_slot
->power_state_check
= false;
662 php_slot
->slot
.ops
= &php_slot_ops
;
664 INIT_LIST_HEAD(&php_slot
->children
);
665 INIT_LIST_HEAD(&php_slot
->link
);
670 static int pnv_php_register_slot(struct pnv_php_slot
*php_slot
)
672 struct pnv_php_slot
*parent
;
673 struct device_node
*dn
= php_slot
->dn
;
677 /* Check if the slot is registered or not */
678 parent
= pnv_php_find_slot(php_slot
->dn
);
680 pnv_php_put_slot(parent
);
684 /* Register PCI slot */
685 ret
= pci_hp_register(&php_slot
->slot
, php_slot
->bus
,
686 php_slot
->slot_no
, php_slot
->name
);
688 SLOT_WARN(php_slot
, "Error %d registering slot\n", ret
);
692 /* Attach to the parent's child list or global list */
693 while ((dn
= of_get_parent(dn
))) {
699 parent
= pnv_php_find_slot(dn
);
708 spin_lock_irqsave(&pnv_php_lock
, flags
);
709 php_slot
->parent
= parent
;
711 list_add_tail(&php_slot
->link
, &parent
->children
);
713 list_add_tail(&php_slot
->link
, &pnv_php_slot_list
);
714 spin_unlock_irqrestore(&pnv_php_lock
, flags
);
716 php_slot
->state
= PNV_PHP_STATE_REGISTERED
;
720 static int pnv_php_enable_msix(struct pnv_php_slot
*php_slot
)
722 struct pci_dev
*pdev
= php_slot
->pdev
;
723 struct msix_entry entry
;
727 /* Get total number of MSIx entries */
728 nr_entries
= pci_msix_vec_count(pdev
);
732 /* Check hotplug MSIx entry is in range */
733 pcie_capability_read_word(pdev
, PCI_EXP_FLAGS
, &pcie_flag
);
734 entry
.entry
= FIELD_GET(PCI_EXP_FLAGS_IRQ
, pcie_flag
);
735 if (entry
.entry
>= nr_entries
)
739 ret
= pci_enable_msix_exact(pdev
, &entry
, 1);
741 SLOT_WARN(php_slot
, "Error %d enabling MSIx\n", ret
);
748 static void pnv_php_event_handler(struct work_struct
*work
)
750 struct pnv_php_event
*event
=
751 container_of(work
, struct pnv_php_event
, work
);
752 struct pnv_php_slot
*php_slot
= event
->php_slot
;
755 pnv_php_enable_slot(&php_slot
->slot
);
757 pnv_php_disable_slot(&php_slot
->slot
);
762 static irqreturn_t
pnv_php_interrupt(int irq
, void *data
)
764 struct pnv_php_slot
*php_slot
= data
;
765 struct pci_dev
*pchild
, *pdev
= php_slot
->pdev
;
766 struct eeh_dev
*edev
;
768 struct pnv_php_event
*event
;
775 pcie_capability_read_word(pdev
, PCI_EXP_SLTSTA
, &sts
);
776 sts
&= (PCI_EXP_SLTSTA_PDC
| PCI_EXP_SLTSTA_DLLSC
);
777 pcie_capability_write_word(pdev
, PCI_EXP_SLTSTA
, sts
);
779 pci_dbg(pdev
, "PCI slot [%s]: HP int! DLAct: %d, PresDet: %d\n",
781 !!(sts
& PCI_EXP_SLTSTA_DLLSC
),
782 !!(sts
& PCI_EXP_SLTSTA_PDC
));
784 if (sts
& PCI_EXP_SLTSTA_DLLSC
) {
785 pcie_capability_read_word(pdev
, PCI_EXP_LNKSTA
, &lsts
);
786 added
= !!(lsts
& PCI_EXP_LNKSTA_DLLLA
);
787 } else if (!(php_slot
->flags
& PNV_PHP_FLAG_BROKEN_PDC
) &&
788 (sts
& PCI_EXP_SLTSTA_PDC
)) {
789 ret
= pnv_pci_get_presence_state(php_slot
->id
, &presence
);
792 "PCI slot [%s] error %d getting presence (0x%04x), to retry the operation.\n",
793 php_slot
->name
, ret
, sts
);
797 added
= !!(presence
== OPAL_PCI_SLOT_PRESENT
);
799 pci_dbg(pdev
, "PCI slot [%s]: Spurious IRQ?\n", php_slot
->name
);
803 /* Freeze the removed PE to avoid unexpected error reporting */
805 pchild
= list_first_entry_or_null(&php_slot
->bus
->devices
,
806 struct pci_dev
, bus_list
);
807 edev
= pchild
? pci_dev_to_eeh_dev(pchild
) : NULL
;
808 pe
= edev
? edev
->pe
: NULL
;
810 eeh_serialize_lock(&flags
);
811 eeh_pe_mark_isolated(pe
);
812 eeh_serialize_unlock(flags
);
813 eeh_pe_set_option(pe
, EEH_OPT_FREEZE_PE
);
818 * The PE is left in frozen state if the event is missed. It's
819 * fine as the PCI devices (PE) aren't functional any more.
821 event
= kzalloc(sizeof(*event
), GFP_ATOMIC
);
824 "PCI slot [%s] missed hotplug event 0x%04x\n",
825 php_slot
->name
, sts
);
829 pci_info(pdev
, "PCI slot [%s] %s (IRQ: %d)\n",
830 php_slot
->name
, added
? "added" : "removed", irq
);
831 INIT_WORK(&event
->work
, pnv_php_event_handler
);
832 event
->added
= added
;
833 event
->php_slot
= php_slot
;
834 queue_work(php_slot
->wq
, &event
->work
);
839 static void pnv_php_init_irq(struct pnv_php_slot
*php_slot
, int irq
)
841 struct pci_dev
*pdev
= php_slot
->pdev
;
846 /* Allocate workqueue */
847 php_slot
->wq
= alloc_workqueue("pciehp-%s", 0, 0, php_slot
->name
);
849 SLOT_WARN(php_slot
, "Cannot alloc workqueue\n");
850 pnv_php_disable_irq(php_slot
, true);
854 /* Check PDC (Presence Detection Change) is broken or not */
855 ret
= of_property_read_u32(php_slot
->dn
, "ibm,slot-broken-pdc",
857 if (!ret
&& broken_pdc
)
858 php_slot
->flags
|= PNV_PHP_FLAG_BROKEN_PDC
;
860 /* Clear pending interrupts */
861 pcie_capability_read_word(pdev
, PCI_EXP_SLTSTA
, &sts
);
862 if (php_slot
->flags
& PNV_PHP_FLAG_BROKEN_PDC
)
863 sts
|= PCI_EXP_SLTSTA_DLLSC
;
865 sts
|= (PCI_EXP_SLTSTA_PDC
| PCI_EXP_SLTSTA_DLLSC
);
866 pcie_capability_write_word(pdev
, PCI_EXP_SLTSTA
, sts
);
868 /* Request the interrupt */
869 ret
= request_irq(irq
, pnv_php_interrupt
, IRQF_SHARED
,
870 php_slot
->name
, php_slot
);
872 pnv_php_disable_irq(php_slot
, true);
873 SLOT_WARN(php_slot
, "Error %d enabling IRQ %d\n", ret
, irq
);
877 /* Enable the interrupts */
878 pcie_capability_read_word(pdev
, PCI_EXP_SLTCTL
, &ctrl
);
879 if (php_slot
->flags
& PNV_PHP_FLAG_BROKEN_PDC
) {
880 ctrl
&= ~PCI_EXP_SLTCTL_PDCE
;
881 ctrl
|= (PCI_EXP_SLTCTL_HPIE
|
882 PCI_EXP_SLTCTL_DLLSCE
);
884 ctrl
|= (PCI_EXP_SLTCTL_HPIE
|
885 PCI_EXP_SLTCTL_PDCE
|
886 PCI_EXP_SLTCTL_DLLSCE
);
888 pcie_capability_write_word(pdev
, PCI_EXP_SLTCTL
, ctrl
);
890 /* The interrupt is initialized successfully when @irq is valid */
894 static void pnv_php_enable_irq(struct pnv_php_slot
*php_slot
)
896 struct pci_dev
*pdev
= php_slot
->pdev
;
900 * The MSI/MSIx interrupt might have been occupied by other
901 * drivers. Don't populate the surprise hotplug capability
904 if (pci_dev_msi_enabled(pdev
))
907 ret
= pci_enable_device(pdev
);
909 SLOT_WARN(php_slot
, "Error %d enabling device\n", ret
);
913 pci_set_master(pdev
);
915 /* Enable MSIx interrupt */
916 irq
= pnv_php_enable_msix(php_slot
);
918 pnv_php_init_irq(php_slot
, irq
);
923 * Use MSI if MSIx doesn't work. Fail back to legacy INTx
924 * if MSI doesn't work either
926 ret
= pci_enable_msi(pdev
);
927 if (!ret
|| pdev
->irq
) {
929 pnv_php_init_irq(php_slot
, irq
);
933 static int pnv_php_register_one(struct device_node
*dn
)
935 struct pnv_php_slot
*php_slot
;
939 /* Check if it's hotpluggable slot */
940 ret
= of_property_read_u32(dn
, "ibm,slot-pluggable", &prop32
);
944 ret
= of_property_read_u32(dn
, "ibm,reset-by-firmware", &prop32
);
948 php_slot
= pnv_php_alloc_slot(dn
);
952 ret
= pnv_php_register_slot(php_slot
);
956 ret
= pnv_php_enable(php_slot
, false);
958 goto unregister_slot
;
960 /* Enable interrupt if the slot supports surprise hotplug */
961 ret
= of_property_read_u32(dn
, "ibm,slot-surprise-pluggable", &prop32
);
963 pnv_php_enable_irq(php_slot
);
968 pnv_php_unregister_one(php_slot
->dn
);
970 pnv_php_put_slot(php_slot
);
974 static void pnv_php_register(struct device_node
*dn
)
976 struct device_node
*child
;
979 * The parent slots should be registered before their
982 for_each_child_of_node(dn
, child
) {
983 pnv_php_register_one(child
);
984 pnv_php_register(child
);
988 static void pnv_php_unregister_one(struct device_node
*dn
)
990 struct pnv_php_slot
*php_slot
;
992 php_slot
= pnv_php_find_slot(dn
);
996 php_slot
->state
= PNV_PHP_STATE_OFFLINE
;
997 pci_hp_deregister(&php_slot
->slot
);
998 pnv_php_release(php_slot
);
999 pnv_php_put_slot(php_slot
);
1002 static void pnv_php_unregister(struct device_node
*dn
)
1004 struct device_node
*child
;
1006 /* The child slots should go before their parent slots */
1007 for_each_child_of_node(dn
, child
) {
1008 pnv_php_unregister(child
);
1009 pnv_php_unregister_one(child
);
1013 static int __init
pnv_php_init(void)
1015 struct device_node
*dn
;
1017 pr_info(DRIVER_DESC
" version: " DRIVER_VERSION
"\n");
1018 for_each_compatible_node(dn
, NULL
, "ibm,ioda2-phb")
1019 pnv_php_register(dn
);
1021 for_each_compatible_node(dn
, NULL
, "ibm,ioda3-phb")
1022 pnv_php_register(dn
);
1024 for_each_compatible_node(dn
, NULL
, "ibm,ioda2-npu2-opencapi-phb")
1025 pnv_php_register_one(dn
); /* slot directly under the PHB */
1029 static void __exit
pnv_php_exit(void)
1031 struct device_node
*dn
;
1033 for_each_compatible_node(dn
, NULL
, "ibm,ioda2-phb")
1034 pnv_php_unregister(dn
);
1036 for_each_compatible_node(dn
, NULL
, "ibm,ioda3-phb")
1037 pnv_php_unregister(dn
);
1039 for_each_compatible_node(dn
, NULL
, "ibm,ioda2-npu2-opencapi-phb")
1040 pnv_php_unregister_one(dn
); /* slot directly under the PHB */
1043 module_init(pnv_php_init
);
1044 module_exit(pnv_php_exit
);
1046 MODULE_VERSION(DRIVER_VERSION
);
1047 MODULE_LICENSE("GPL v2");
1048 MODULE_AUTHOR(DRIVER_AUTHOR
);
1049 MODULE_DESCRIPTION(DRIVER_DESC
);