|
22 | 22 | #define DRIVER_AUTHOR "Gavin Shan, IBM Corporation" |
23 | 23 | #define DRIVER_DESC "PowerPC PowerNV PCI Hotplug Driver" |
24 | 24 |
|
| 25 | +struct pnv_php_event { |
| 26 | + bool added; |
| 27 | + struct pnv_php_slot *php_slot; |
| 28 | + struct work_struct work; |
| 29 | +}; |
| 30 | + |
25 | 31 | static LIST_HEAD(pnv_php_slot_list); |
26 | 32 | static DEFINE_SPINLOCK(pnv_php_lock); |
27 | 33 |
|
28 | 34 | static void pnv_php_register(struct device_node *dn); |
29 | 35 | static void pnv_php_unregister_one(struct device_node *dn); |
30 | 36 | static void pnv_php_unregister(struct device_node *dn); |
31 | 37 |
|
| 38 | +static void pnv_php_disable_irq(struct pnv_php_slot *php_slot) |
| 39 | +{ |
| 40 | + struct pci_dev *pdev = php_slot->pdev; |
| 41 | + u16 ctrl; |
| 42 | + |
| 43 | + if (php_slot->irq > 0) { |
| 44 | + pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &ctrl); |
| 45 | + ctrl &= ~(PCI_EXP_SLTCTL_HPIE | |
| 46 | + PCI_EXP_SLTCTL_PDCE | |
| 47 | + PCI_EXP_SLTCTL_DLLSCE); |
| 48 | + pcie_capability_write_word(pdev, PCI_EXP_SLTCTL, ctrl); |
| 49 | + |
| 50 | + free_irq(php_slot->irq, php_slot); |
| 51 | + php_slot->irq = 0; |
| 52 | + } |
| 53 | + |
| 54 | + if (php_slot->wq) { |
| 55 | + destroy_workqueue(php_slot->wq); |
| 56 | + php_slot->wq = NULL; |
| 57 | + } |
| 58 | + |
| 59 | + if (pdev->msix_enabled) |
| 60 | + pci_disable_msix(pdev); |
| 61 | + else if (pdev->msi_enabled) |
| 62 | + pci_disable_msi(pdev); |
| 63 | +} |
| 64 | + |
32 | 65 | static void pnv_php_free_slot(struct kref *kref) |
33 | 66 | { |
34 | 67 | struct pnv_php_slot *php_slot = container_of(kref, |
35 | 68 | struct pnv_php_slot, kref); |
36 | 69 |
|
37 | 70 | WARN_ON(!list_empty(&php_slot->children)); |
| 71 | + pnv_php_disable_irq(php_slot); |
38 | 72 | kfree(php_slot->name); |
39 | 73 | kfree(php_slot); |
40 | 74 | } |
@@ -609,6 +643,181 @@ static int pnv_php_register_slot(struct pnv_php_slot *php_slot) |
609 | 643 | return 0; |
610 | 644 | } |
611 | 645 |
|
| 646 | +static int pnv_php_enable_msix(struct pnv_php_slot *php_slot) |
| 647 | +{ |
| 648 | + struct pci_dev *pdev = php_slot->pdev; |
| 649 | + struct msix_entry entry; |
| 650 | + int nr_entries, ret; |
| 651 | + u16 pcie_flag; |
| 652 | + |
| 653 | + /* Get total number of MSIx entries */ |
| 654 | + nr_entries = pci_msix_vec_count(pdev); |
| 655 | + if (nr_entries < 0) |
| 656 | + return nr_entries; |
| 657 | + |
| 658 | + /* Check hotplug MSIx entry is in range */ |
| 659 | + pcie_capability_read_word(pdev, PCI_EXP_FLAGS, &pcie_flag); |
| 660 | + entry.entry = (pcie_flag & PCI_EXP_FLAGS_IRQ) >> 9; |
| 661 | + if (entry.entry >= nr_entries) |
| 662 | + return -ERANGE; |
| 663 | + |
| 664 | + /* Enable MSIx */ |
| 665 | + ret = pci_enable_msix_exact(pdev, &entry, 1); |
| 666 | + if (ret) { |
| 667 | + dev_warn(&pdev->dev, "Error %d enabling MSIx\n", ret); |
| 668 | + return ret; |
| 669 | + } |
| 670 | + |
| 671 | + return entry.vector; |
| 672 | +} |
| 673 | + |
| 674 | +static void pnv_php_event_handler(struct work_struct *work) |
| 675 | +{ |
| 676 | + struct pnv_php_event *event = |
| 677 | + container_of(work, struct pnv_php_event, work); |
| 678 | + struct pnv_php_slot *php_slot = event->php_slot; |
| 679 | + |
| 680 | + if (event->added) |
| 681 | + pnv_php_enable_slot(&php_slot->slot); |
| 682 | + else |
| 683 | + pnv_php_disable_slot(&php_slot->slot); |
| 684 | + |
| 685 | + kfree(event); |
| 686 | +} |
| 687 | + |
| 688 | +static irqreturn_t pnv_php_interrupt(int irq, void *data) |
| 689 | +{ |
| 690 | + struct pnv_php_slot *php_slot = data; |
| 691 | + struct pci_dev *pchild, *pdev = php_slot->pdev; |
| 692 | + struct eeh_dev *edev; |
| 693 | + struct eeh_pe *pe; |
| 694 | + struct pnv_php_event *event; |
| 695 | + u16 sts, lsts; |
| 696 | + u8 presence; |
| 697 | + bool added; |
| 698 | + unsigned long flags; |
| 699 | + int ret; |
| 700 | + |
| 701 | + pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &sts); |
| 702 | + sts &= (PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_DLLSC); |
| 703 | + pcie_capability_write_word(pdev, PCI_EXP_SLTSTA, sts); |
| 704 | + if (sts & PCI_EXP_SLTSTA_DLLSC) { |
| 705 | + pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lsts); |
| 706 | + added = !!(lsts & PCI_EXP_LNKSTA_DLLLA); |
| 707 | + } else if (sts & PCI_EXP_SLTSTA_PDC) { |
| 708 | + ret = pnv_pci_get_presence_state(php_slot->id, &presence); |
| 709 | + if (!ret) |
| 710 | + return IRQ_HANDLED; |
| 711 | + added = !!(presence == OPAL_PCI_SLOT_PRESENT); |
| 712 | + } else { |
| 713 | + return IRQ_NONE; |
| 714 | + } |
| 715 | + |
| 716 | + /* Freeze the removed PE to avoid unexpected error reporting */ |
| 717 | + if (!added) { |
| 718 | + pchild = list_first_entry_or_null(&php_slot->bus->devices, |
| 719 | + struct pci_dev, bus_list); |
| 720 | + edev = pchild ? pci_dev_to_eeh_dev(pchild) : NULL; |
| 721 | + pe = edev ? edev->pe : NULL; |
| 722 | + if (pe) { |
| 723 | + eeh_serialize_lock(&flags); |
| 724 | + eeh_pe_state_mark(pe, EEH_PE_ISOLATED); |
| 725 | + eeh_serialize_unlock(flags); |
| 726 | + eeh_pe_set_option(pe, EEH_OPT_FREEZE_PE); |
| 727 | + } |
| 728 | + } |
| 729 | + |
| 730 | + /* |
| 731 | + * The PE is left in frozen state if the event is missed. It's |
| 732 | + * fine as the PCI devices (PE) aren't functional any more. |
| 733 | + */ |
| 734 | + event = kzalloc(sizeof(*event), GFP_ATOMIC); |
| 735 | + if (!event) { |
| 736 | + dev_warn(&pdev->dev, "PCI slot [%s] missed hotplug event 0x%04x\n", |
| 737 | + php_slot->name, sts); |
| 738 | + return IRQ_HANDLED; |
| 739 | + } |
| 740 | + |
| 741 | + dev_info(&pdev->dev, "PCI slot [%s] %s (IRQ: %d)\n", |
| 742 | + php_slot->name, added ? "added" : "removed", irq); |
| 743 | + INIT_WORK(&event->work, pnv_php_event_handler); |
| 744 | + event->added = added; |
| 745 | + event->php_slot = php_slot; |
| 746 | + queue_work(php_slot->wq, &event->work); |
| 747 | + |
| 748 | + return IRQ_HANDLED; |
| 749 | +} |
| 750 | + |
| 751 | +static void pnv_php_init_irq(struct pnv_php_slot *php_slot, int irq) |
| 752 | +{ |
| 753 | + struct pci_dev *pdev = php_slot->pdev; |
| 754 | + u16 sts, ctrl; |
| 755 | + int ret; |
| 756 | + |
| 757 | + /* Allocate workqueue */ |
| 758 | + php_slot->wq = alloc_workqueue("pciehp-%s", 0, 0, php_slot->name); |
| 759 | + if (!php_slot->wq) { |
| 760 | + dev_warn(&pdev->dev, "Cannot alloc workqueue\n"); |
| 761 | + pnv_php_disable_irq(php_slot); |
| 762 | + return; |
| 763 | + } |
| 764 | + |
| 765 | + /* Clear pending interrupts */ |
| 766 | + pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &sts); |
| 767 | + sts |= (PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_DLLSC); |
| 768 | + pcie_capability_write_word(pdev, PCI_EXP_SLTSTA, sts); |
| 769 | + |
| 770 | + /* Request the interrupt */ |
| 771 | + ret = request_irq(irq, pnv_php_interrupt, IRQF_SHARED, |
| 772 | + php_slot->name, php_slot); |
| 773 | + if (ret) { |
| 774 | + pnv_php_disable_irq(php_slot); |
| 775 | + dev_warn(&pdev->dev, "Error %d enabling IRQ %d\n", ret, irq); |
| 776 | + return; |
| 777 | + } |
| 778 | + |
| 779 | + /* Enable the interrupts */ |
| 780 | + pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &ctrl); |
| 781 | + ctrl |= (PCI_EXP_SLTCTL_HPIE | |
| 782 | + PCI_EXP_SLTCTL_PDCE | |
| 783 | + PCI_EXP_SLTCTL_DLLSCE); |
| 784 | + pcie_capability_write_word(pdev, PCI_EXP_SLTCTL, ctrl); |
| 785 | + |
| 786 | + /* The interrupt is initialized successfully when @irq is valid */ |
| 787 | + php_slot->irq = irq; |
| 788 | +} |
| 789 | + |
| 790 | +static void pnv_php_enable_irq(struct pnv_php_slot *php_slot) |
| 791 | +{ |
| 792 | + struct pci_dev *pdev = php_slot->pdev; |
| 793 | + int irq, ret; |
| 794 | + |
| 795 | + ret = pci_enable_device(pdev); |
| 796 | + if (ret) { |
| 797 | + dev_warn(&pdev->dev, "Error %d enabling device\n", ret); |
| 798 | + return; |
| 799 | + } |
| 800 | + |
| 801 | + pci_set_master(pdev); |
| 802 | + |
| 803 | + /* Enable MSIx interrupt */ |
| 804 | + irq = pnv_php_enable_msix(php_slot); |
| 805 | + if (irq > 0) { |
| 806 | + pnv_php_init_irq(php_slot, irq); |
| 807 | + return; |
| 808 | + } |
| 809 | + |
| 810 | + /* |
| 811 | + * Use MSI if MSIx doesn't work. Fail back to legacy INTx |
| 812 | + * if MSI doesn't work either |
| 813 | + */ |
| 814 | + ret = pci_enable_msi(pdev); |
| 815 | + if (!ret || pdev->irq) { |
| 816 | + irq = pdev->irq; |
| 817 | + pnv_php_init_irq(php_slot, irq); |
| 818 | + } |
| 819 | +} |
| 820 | + |
612 | 821 | static int pnv_php_register_one(struct device_node *dn) |
613 | 822 | { |
614 | 823 | struct pnv_php_slot *php_slot; |
@@ -636,6 +845,11 @@ static int pnv_php_register_one(struct device_node *dn) |
636 | 845 | if (ret) |
637 | 846 | goto unregister_slot; |
638 | 847 |
|
| 848 | + /* Enable interrupt if the slot supports surprise hotplug */ |
| 849 | + prop32 = of_get_property(dn, "ibm,slot-surprise-pluggable", NULL); |
| 850 | + if (prop32 && of_read_number(prop32, 1)) |
| 851 | + pnv_php_enable_irq(php_slot); |
| 852 | + |
639 | 853 | return 0; |
640 | 854 |
|
641 | 855 | unregister_slot: |
|
0 commit comments