aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/pci/hotplug/pciehp_hpc.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/pci/hotplug/pciehp_hpc.c')
-rw-r--r--drivers/pci/hotplug/pciehp_hpc.c565
1 files changed, 175 insertions, 390 deletions
diff --git a/drivers/pci/hotplug/pciehp_hpc.c b/drivers/pci/hotplug/pciehp_hpc.c
index b4bbd07d1e3..891f81a0400 100644
--- a/drivers/pci/hotplug/pciehp_hpc.c
+++ b/drivers/pci/hotplug/pciehp_hpc.c
@@ -221,6 +221,32 @@ static void start_int_poll_timer(struct controller *ctrl, int sec)
221 add_timer(&ctrl->poll_timer); 221 add_timer(&ctrl->poll_timer);
222} 222}
223 223
224static inline int pciehp_request_irq(struct controller *ctrl)
225{
226 int retval, irq = ctrl->pci_dev->irq;
227
228 /* Install interrupt polling timer. Start with 10 sec delay */
229 if (pciehp_poll_mode) {
230 init_timer(&ctrl->poll_timer);
231 start_int_poll_timer(ctrl, 10);
232 return 0;
233 }
234
235 /* Installs the interrupt handler */
236 retval = request_irq(irq, pcie_isr, IRQF_SHARED, MY_NAME, ctrl);
237 if (retval)
238 err("Cannot get irq %d for the hotplug controller\n", irq);
239 return retval;
240}
241
242static inline void pciehp_free_irq(struct controller *ctrl)
243{
244 if (pciehp_poll_mode)
245 del_timer_sync(&ctrl->poll_timer);
246 else
247 free_irq(ctrl->pci_dev->irq, ctrl);
248}
249
224static inline int pcie_wait_cmd(struct controller *ctrl) 250static inline int pcie_wait_cmd(struct controller *ctrl)
225{ 251{
226 int retval = 0; 252 int retval = 0;
@@ -242,17 +268,15 @@ static inline int pcie_wait_cmd(struct controller *ctrl)
242 268
243/** 269/**
244 * pcie_write_cmd - Issue controller command 270 * pcie_write_cmd - Issue controller command
245 * @slot: slot to which the command is issued 271 * @ctrl: controller to which the command is issued
246 * @cmd: command value written to slot control register 272 * @cmd: command value written to slot control register
247 * @mask: bitmask of slot control register to be modified 273 * @mask: bitmask of slot control register to be modified
248 */ 274 */
249static int pcie_write_cmd(struct slot *slot, u16 cmd, u16 mask) 275static int pcie_write_cmd(struct controller *ctrl, u16 cmd, u16 mask)
250{ 276{
251 struct controller *ctrl = slot->ctrl;
252 int retval = 0; 277 int retval = 0;
253 u16 slot_status; 278 u16 slot_status;
254 u16 slot_ctrl; 279 u16 slot_ctrl;
255 unsigned long flags;
256 280
257 mutex_lock(&ctrl->ctrl_lock); 281 mutex_lock(&ctrl->ctrl_lock);
258 282
@@ -270,24 +294,24 @@ static int pcie_write_cmd(struct slot *slot, u16 cmd, u16 mask)
270 __func__); 294 __func__);
271 } 295 }
272 296
273 spin_lock_irqsave(&ctrl->lock, flags);
274 retval = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl); 297 retval = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
275 if (retval) { 298 if (retval) {
276 err("%s: Cannot read SLOTCTRL register\n", __func__); 299 err("%s: Cannot read SLOTCTRL register\n", __func__);
277 goto out_spin_unlock; 300 goto out;
278 } 301 }
279 302
280 slot_ctrl &= ~mask; 303 slot_ctrl &= ~mask;
281 slot_ctrl |= ((cmd & mask) | CMD_CMPL_INTR_ENABLE); 304 slot_ctrl |= (cmd & mask);
305 /* Don't enable command completed if caller is changing it. */
306 if (!(mask & CMD_CMPL_INTR_ENABLE))
307 slot_ctrl |= CMD_CMPL_INTR_ENABLE;
282 308
283 ctrl->cmd_busy = 1; 309 ctrl->cmd_busy = 1;
310 smp_mb();
284 retval = pciehp_writew(ctrl, SLOTCTRL, slot_ctrl); 311 retval = pciehp_writew(ctrl, SLOTCTRL, slot_ctrl);
285 if (retval) 312 if (retval)
286 err("%s: Cannot write to SLOTCTRL register\n", __func__); 313 err("%s: Cannot write to SLOTCTRL register\n", __func__);
287 314
288 out_spin_unlock:
289 spin_unlock_irqrestore(&ctrl->lock, flags);
290
291 /* 315 /*
292 * Wait for command completion. 316 * Wait for command completion.
293 */ 317 */
@@ -467,12 +491,7 @@ static int hpc_toggle_emi(struct slot *slot)
467 491
468 slot_cmd = EMI_CTRL; 492 slot_cmd = EMI_CTRL;
469 cmd_mask = EMI_CTRL; 493 cmd_mask = EMI_CTRL;
470 if (!pciehp_poll_mode) { 494 rc = pcie_write_cmd(slot->ctrl, slot_cmd, cmd_mask);
471 slot_cmd = slot_cmd | HP_INTR_ENABLE;
472 cmd_mask = cmd_mask | HP_INTR_ENABLE;
473 }
474
475 rc = pcie_write_cmd(slot, slot_cmd, cmd_mask);
476 slot->last_emi_toggle = get_seconds(); 495 slot->last_emi_toggle = get_seconds();
477 496
478 return rc; 497 return rc;
@@ -499,12 +518,7 @@ static int hpc_set_attention_status(struct slot *slot, u8 value)
499 default: 518 default:
500 return -1; 519 return -1;
501 } 520 }
502 if (!pciehp_poll_mode) { 521 rc = pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
503 slot_cmd = slot_cmd | HP_INTR_ENABLE;
504 cmd_mask = cmd_mask | HP_INTR_ENABLE;
505 }
506
507 rc = pcie_write_cmd(slot, slot_cmd, cmd_mask);
508 dbg("%s: SLOTCTRL %x write cmd %x\n", 522 dbg("%s: SLOTCTRL %x write cmd %x\n",
509 __func__, ctrl->cap_base + SLOTCTRL, slot_cmd); 523 __func__, ctrl->cap_base + SLOTCTRL, slot_cmd);
510 524
@@ -519,13 +533,7 @@ static void hpc_set_green_led_on(struct slot *slot)
519 533
520 slot_cmd = 0x0100; 534 slot_cmd = 0x0100;
521 cmd_mask = PWR_LED_CTRL; 535 cmd_mask = PWR_LED_CTRL;
522 if (!pciehp_poll_mode) { 536 pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
523 slot_cmd = slot_cmd | HP_INTR_ENABLE;
524 cmd_mask = cmd_mask | HP_INTR_ENABLE;
525 }
526
527 pcie_write_cmd(slot, slot_cmd, cmd_mask);
528
529 dbg("%s: SLOTCTRL %x write cmd %x\n", 537 dbg("%s: SLOTCTRL %x write cmd %x\n",
530 __func__, ctrl->cap_base + SLOTCTRL, slot_cmd); 538 __func__, ctrl->cap_base + SLOTCTRL, slot_cmd);
531} 539}
@@ -538,12 +546,7 @@ static void hpc_set_green_led_off(struct slot *slot)
538 546
539 slot_cmd = 0x0300; 547 slot_cmd = 0x0300;
540 cmd_mask = PWR_LED_CTRL; 548 cmd_mask = PWR_LED_CTRL;
541 if (!pciehp_poll_mode) { 549 pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
542 slot_cmd = slot_cmd | HP_INTR_ENABLE;
543 cmd_mask = cmd_mask | HP_INTR_ENABLE;
544 }
545
546 pcie_write_cmd(slot, slot_cmd, cmd_mask);
547 dbg("%s: SLOTCTRL %x write cmd %x\n", 550 dbg("%s: SLOTCTRL %x write cmd %x\n",
548 __func__, ctrl->cap_base + SLOTCTRL, slot_cmd); 551 __func__, ctrl->cap_base + SLOTCTRL, slot_cmd);
549} 552}
@@ -556,23 +559,19 @@ static void hpc_set_green_led_blink(struct slot *slot)
556 559
557 slot_cmd = 0x0200; 560 slot_cmd = 0x0200;
558 cmd_mask = PWR_LED_CTRL; 561 cmd_mask = PWR_LED_CTRL;
559 if (!pciehp_poll_mode) { 562 pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
560 slot_cmd = slot_cmd | HP_INTR_ENABLE;
561 cmd_mask = cmd_mask | HP_INTR_ENABLE;
562 }
563
564 pcie_write_cmd(slot, slot_cmd, cmd_mask);
565
566 dbg("%s: SLOTCTRL %x write cmd %x\n", 563 dbg("%s: SLOTCTRL %x write cmd %x\n",
567 __func__, ctrl->cap_base + SLOTCTRL, slot_cmd); 564 __func__, ctrl->cap_base + SLOTCTRL, slot_cmd);
568} 565}
569 566
570static void hpc_release_ctlr(struct controller *ctrl) 567static void hpc_release_ctlr(struct controller *ctrl)
571{ 568{
572 if (pciehp_poll_mode) 569 /* Mask Hot-plug Interrupt Enable */
573 del_timer(&ctrl->poll_timer); 570 if (pcie_write_cmd(ctrl, 0, HP_INTR_ENABLE | CMD_CMPL_INTR_ENABLE))
574 else 571 err("%s: Cannot mask hotplut interrupt enable\n", __func__);
575 free_irq(ctrl->pci_dev->irq, ctrl); 572
573 /* Free interrupt handler or interrupt polling timer */
574 pciehp_free_irq(ctrl);
576 575
577 /* 576 /*
578 * If this is the last controller to be released, destroy the 577 * If this is the last controller to be released, destroy the
@@ -612,19 +611,13 @@ static int hpc_power_on_slot(struct slot * slot)
612 cmd_mask = PWR_CTRL; 611 cmd_mask = PWR_CTRL;
613 /* Enable detection that we turned off at slot power-off time */ 612 /* Enable detection that we turned off at slot power-off time */
614 if (!pciehp_poll_mode) { 613 if (!pciehp_poll_mode) {
615 slot_cmd = slot_cmd | 614 slot_cmd |= (PWR_FAULT_DETECT_ENABLE | MRL_DETECT_ENABLE |
616 PWR_FAULT_DETECT_ENABLE | 615 PRSN_DETECT_ENABLE);
617 MRL_DETECT_ENABLE | 616 cmd_mask |= (PWR_FAULT_DETECT_ENABLE | MRL_DETECT_ENABLE |
618 PRSN_DETECT_ENABLE | 617 PRSN_DETECT_ENABLE);
619 HP_INTR_ENABLE;
620 cmd_mask = cmd_mask |
621 PWR_FAULT_DETECT_ENABLE |
622 MRL_DETECT_ENABLE |
623 PRSN_DETECT_ENABLE |
624 HP_INTR_ENABLE;
625 } 618 }
626 619
627 retval = pcie_write_cmd(slot, slot_cmd, cmd_mask); 620 retval = pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
628 621
629 if (retval) { 622 if (retval) {
630 err("%s: Write %x command failed!\n", __func__, slot_cmd); 623 err("%s: Write %x command failed!\n", __func__, slot_cmd);
@@ -697,18 +690,13 @@ static int hpc_power_off_slot(struct slot * slot)
697 * till the slot is powered on again. 690 * till the slot is powered on again.
698 */ 691 */
699 if (!pciehp_poll_mode) { 692 if (!pciehp_poll_mode) {
700 slot_cmd = (slot_cmd & 693 slot_cmd &= ~(PWR_FAULT_DETECT_ENABLE | MRL_DETECT_ENABLE |
701 ~PWR_FAULT_DETECT_ENABLE & 694 PRSN_DETECT_ENABLE);
702 ~MRL_DETECT_ENABLE & 695 cmd_mask |= (PWR_FAULT_DETECT_ENABLE | MRL_DETECT_ENABLE |
703 ~PRSN_DETECT_ENABLE) | HP_INTR_ENABLE; 696 PRSN_DETECT_ENABLE);
704 cmd_mask = cmd_mask |
705 PWR_FAULT_DETECT_ENABLE |
706 MRL_DETECT_ENABLE |
707 PRSN_DETECT_ENABLE |
708 HP_INTR_ENABLE;
709 } 697 }
710 698
711 retval = pcie_write_cmd(slot, slot_cmd, cmd_mask); 699 retval = pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
712 if (retval) { 700 if (retval) {
713 err("%s: Write command failed!\n", __func__); 701 err("%s: Write command failed!\n", __func__);
714 retval = -1; 702 retval = -1;
@@ -733,139 +721,56 @@ static int hpc_power_off_slot(struct slot * slot)
733static irqreturn_t pcie_isr(int irq, void *dev_id) 721static irqreturn_t pcie_isr(int irq, void *dev_id)
734{ 722{
735 struct controller *ctrl = (struct controller *)dev_id; 723 struct controller *ctrl = (struct controller *)dev_id;
736 u16 slot_status, intr_detect, intr_loc; 724 u16 detected, intr_loc;
737 u16 temp_word;
738 int hp_slot = 0; /* only 1 slot per PCI Express port */
739 int rc = 0;
740 unsigned long flags;
741
742 rc = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
743 if (rc) {
744 err("%s: Cannot read SLOTSTATUS register\n", __func__);
745 return IRQ_NONE;
746 }
747
748 intr_detect = (ATTN_BUTTN_PRESSED | PWR_FAULT_DETECTED |
749 MRL_SENS_CHANGED | PRSN_DETECT_CHANGED | CMD_COMPLETED);
750
751 intr_loc = slot_status & intr_detect;
752
753 /* Check to see if it was our interrupt */
754 if ( !intr_loc )
755 return IRQ_NONE;
756 725
757 dbg("%s: intr_loc %x\n", __func__, intr_loc); 726 /*
758 /* Mask Hot-plug Interrupt Enable */ 727 * In order to guarantee that all interrupt events are
759 if (!pciehp_poll_mode) { 728 * serviced, we need to re-inspect Slot Status register after
760 spin_lock_irqsave(&ctrl->lock, flags); 729 * clearing what is presumed to be the last pending interrupt.
761 rc = pciehp_readw(ctrl, SLOTCTRL, &temp_word); 730 */
762 if (rc) { 731 intr_loc = 0;
763 err("%s: Cannot read SLOT_CTRL register\n", 732 do {
764 __func__); 733 if (pciehp_readw(ctrl, SLOTSTATUS, &detected)) {
765 spin_unlock_irqrestore(&ctrl->lock, flags); 734 err("%s: Cannot read SLOTSTATUS\n", __func__);
766 return IRQ_NONE; 735 return IRQ_NONE;
767 } 736 }
768 737
769 dbg("%s: pciehp_readw(SLOTCTRL) with value %x\n", 738 detected &= (ATTN_BUTTN_PRESSED | PWR_FAULT_DETECTED |
770 __func__, temp_word); 739 MRL_SENS_CHANGED | PRSN_DETECT_CHANGED |
771 temp_word = (temp_word & ~HP_INTR_ENABLE & 740 CMD_COMPLETED);
772 ~CMD_CMPL_INTR_ENABLE) | 0x00; 741 intr_loc |= detected;
773 rc = pciehp_writew(ctrl, SLOTCTRL, temp_word); 742 if (!intr_loc)
774 if (rc) {
775 err("%s: Cannot write to SLOTCTRL register\n",
776 __func__);
777 spin_unlock_irqrestore(&ctrl->lock, flags);
778 return IRQ_NONE; 743 return IRQ_NONE;
779 } 744 if (pciehp_writew(ctrl, SLOTSTATUS, detected)) {
780 spin_unlock_irqrestore(&ctrl->lock, flags); 745 err("%s: Cannot write to SLOTSTATUS\n", __func__);
781
782 rc = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
783 if (rc) {
784 err("%s: Cannot read SLOT_STATUS register\n",
785 __func__);
786 return IRQ_NONE; 746 return IRQ_NONE;
787 } 747 }
788 dbg("%s: pciehp_readw(SLOTSTATUS) with value %x\n", 748 } while (detected);
789 __func__, slot_status);
790 749
791 /* Clear command complete interrupt caused by this write */ 750 dbg("%s: intr_loc %x\n", __FUNCTION__, intr_loc);
792 temp_word = 0x1f;
793 rc = pciehp_writew(ctrl, SLOTSTATUS, temp_word);
794 if (rc) {
795 err("%s: Cannot write to SLOTSTATUS register\n",
796 __func__);
797 return IRQ_NONE;
798 }
799 }
800 751
752 /* Check Command Complete Interrupt Pending */
801 if (intr_loc & CMD_COMPLETED) { 753 if (intr_loc & CMD_COMPLETED) {
802 /*
803 * Command Complete Interrupt Pending
804 */
805 ctrl->cmd_busy = 0; 754 ctrl->cmd_busy = 0;
755 smp_mb();
806 wake_up_interruptible(&ctrl->queue); 756 wake_up_interruptible(&ctrl->queue);
807 } 757 }
808 758
759 /* Check MRL Sensor Changed */
809 if (intr_loc & MRL_SENS_CHANGED) 760 if (intr_loc & MRL_SENS_CHANGED)
810 pciehp_handle_switch_change(hp_slot, ctrl); 761 pciehp_handle_switch_change(0, ctrl);
811 762
763 /* Check Attention Button Pressed */
812 if (intr_loc & ATTN_BUTTN_PRESSED) 764 if (intr_loc & ATTN_BUTTN_PRESSED)
813 pciehp_handle_attention_button(hp_slot, ctrl); 765 pciehp_handle_attention_button(0, ctrl);
814 766
767 /* Check Presence Detect Changed */
815 if (intr_loc & PRSN_DETECT_CHANGED) 768 if (intr_loc & PRSN_DETECT_CHANGED)
816 pciehp_handle_presence_change(hp_slot, ctrl); 769 pciehp_handle_presence_change(0, ctrl);
817 770
771 /* Check Power Fault Detected */
818 if (intr_loc & PWR_FAULT_DETECTED) 772 if (intr_loc & PWR_FAULT_DETECTED)
819 pciehp_handle_power_fault(hp_slot, ctrl); 773 pciehp_handle_power_fault(0, ctrl);
820
821 /* Clear all events after serving them */
822 temp_word = 0x1F;
823 rc = pciehp_writew(ctrl, SLOTSTATUS, temp_word);
824 if (rc) {
825 err("%s: Cannot write to SLOTSTATUS register\n", __func__);
826 return IRQ_NONE;
827 }
828 /* Unmask Hot-plug Interrupt Enable */
829 if (!pciehp_poll_mode) {
830 spin_lock_irqsave(&ctrl->lock, flags);
831 rc = pciehp_readw(ctrl, SLOTCTRL, &temp_word);
832 if (rc) {
833 err("%s: Cannot read SLOTCTRL register\n",
834 __func__);
835 spin_unlock_irqrestore(&ctrl->lock, flags);
836 return IRQ_NONE;
837 }
838
839 dbg("%s: Unmask Hot-plug Interrupt Enable\n", __func__);
840 temp_word = (temp_word & ~HP_INTR_ENABLE) | HP_INTR_ENABLE;
841
842 rc = pciehp_writew(ctrl, SLOTCTRL, temp_word);
843 if (rc) {
844 err("%s: Cannot write to SLOTCTRL register\n",
845 __func__);
846 spin_unlock_irqrestore(&ctrl->lock, flags);
847 return IRQ_NONE;
848 }
849 spin_unlock_irqrestore(&ctrl->lock, flags);
850
851 rc = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
852 if (rc) {
853 err("%s: Cannot read SLOT_STATUS register\n",
854 __func__);
855 return IRQ_NONE;
856 }
857
858 /* Clear command complete interrupt caused by this write */
859 temp_word = 0x1F;
860 rc = pciehp_writew(ctrl, SLOTSTATUS, temp_word);
861 if (rc) {
862 err("%s: Cannot write to SLOTSTATUS failed\n",
863 __func__);
864 return IRQ_NONE;
865 }
866 dbg("%s: pciehp_writew(SLOTSTATUS) with value %x\n",
867 __func__, temp_word);
868 }
869 774
870 return IRQ_HANDLED; 775 return IRQ_HANDLED;
871} 776}
@@ -1052,7 +957,7 @@ static struct hpc_ops pciehp_hpc_ops = {
1052}; 957};
1053 958
1054#ifdef CONFIG_ACPI 959#ifdef CONFIG_ACPI
1055int pciehp_acpi_get_hp_hw_control_from_firmware(struct pci_dev *dev) 960static int pciehp_acpi_get_hp_hw_control_from_firmware(struct pci_dev *dev)
1056{ 961{
1057 acpi_status status; 962 acpi_status status;
1058 acpi_handle chandle, handle = DEVICE_ACPI_HANDLE(&(dev->dev)); 963 acpi_handle chandle, handle = DEVICE_ACPI_HANDLE(&(dev->dev));
@@ -1112,7 +1017,7 @@ int pciehp_acpi_get_hp_hw_control_from_firmware(struct pci_dev *dev)
1112 break; 1017 break;
1113 } 1018 }
1114 1019
1115 err("Cannot get control of hotplug hardware for pci %s\n", 1020 dbg("Cannot get control of hotplug hardware for pci %s\n",
1116 pci_name(dev)); 1021 pci_name(dev));
1117 1022
1118 kfree(string.pointer); 1023 kfree(string.pointer);
@@ -1123,45 +1028,9 @@ int pciehp_acpi_get_hp_hw_control_from_firmware(struct pci_dev *dev)
1123static int pcie_init_hardware_part1(struct controller *ctrl, 1028static int pcie_init_hardware_part1(struct controller *ctrl,
1124 struct pcie_device *dev) 1029 struct pcie_device *dev)
1125{ 1030{
1126 int rc;
1127 u16 temp_word;
1128 u32 slot_cap;
1129 u16 slot_status;
1130
1131 rc = pciehp_readl(ctrl, SLOTCAP, &slot_cap);
1132 if (rc) {
1133 err("%s: Cannot read SLOTCAP register\n", __func__);
1134 return -1;
1135 }
1136
1137 /* Mask Hot-plug Interrupt Enable */ 1031 /* Mask Hot-plug Interrupt Enable */
1138 rc = pciehp_readw(ctrl, SLOTCTRL, &temp_word); 1032 if (pcie_write_cmd(ctrl, 0, HP_INTR_ENABLE | CMD_CMPL_INTR_ENABLE)) {
1139 if (rc) { 1033 err("%s: Cannot mask hotplug interrupt enable\n", __func__);
1140 err("%s: Cannot read SLOTCTRL register\n", __func__);
1141 return -1;
1142 }
1143
1144 dbg("%s: SLOTCTRL %x value read %x\n",
1145 __func__, ctrl->cap_base + SLOTCTRL, temp_word);
1146 temp_word = (temp_word & ~HP_INTR_ENABLE & ~CMD_CMPL_INTR_ENABLE) |
1147 0x00;
1148
1149 rc = pciehp_writew(ctrl, SLOTCTRL, temp_word);
1150 if (rc) {
1151 err("%s: Cannot write to SLOTCTRL register\n", __func__);
1152 return -1;
1153 }
1154
1155 rc = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
1156 if (rc) {
1157 err("%s: Cannot read SLOTSTATUS register\n", __func__);
1158 return -1;
1159 }
1160
1161 temp_word = 0x1F; /* Clear all events */
1162 rc = pciehp_writew(ctrl, SLOTSTATUS, temp_word);
1163 if (rc) {
1164 err("%s: Cannot write to SLOTSTATUS register\n", __func__);
1165 return -1; 1034 return -1;
1166 } 1035 }
1167 return 0; 1036 return 0;
@@ -1169,205 +1038,125 @@ static int pcie_init_hardware_part1(struct controller *ctrl,
1169 1038
1170int pcie_init_hardware_part2(struct controller *ctrl, struct pcie_device *dev) 1039int pcie_init_hardware_part2(struct controller *ctrl, struct pcie_device *dev)
1171{ 1040{
1172 int rc; 1041 u16 cmd, mask;
1173 u16 temp_word;
1174 u16 intr_enable = 0;
1175 u32 slot_cap;
1176 u16 slot_status;
1177 1042
1178 rc = pciehp_readw(ctrl, SLOTCTRL, &temp_word); 1043 /*
1179 if (rc) { 1044 * We need to clear all events before enabling hotplug interrupt
1180 err("%s: Cannot read SLOTCTRL register\n", __func__); 1045 * notification mechanism in order for hotplug controler to
1181 goto abort; 1046 * generate interrupts.
1182 } 1047 */
1183 1048 if (pciehp_writew(ctrl, SLOTSTATUS, 0x1f)) {
1184 intr_enable = intr_enable | PRSN_DETECT_ENABLE; 1049 err("%s: Cannot write to SLOTSTATUS register\n", __FUNCTION__);
1185 1050 return -1;
1186 rc = pciehp_readl(ctrl, SLOTCAP, &slot_cap);
1187 if (rc) {
1188 err("%s: Cannot read SLOTCAP register\n", __func__);
1189 goto abort;
1190 } 1051 }
1191 1052
1192 if (ATTN_BUTTN(slot_cap)) 1053 cmd = PRSN_DETECT_ENABLE;
1193 intr_enable = intr_enable | ATTN_BUTTN_ENABLE; 1054 if (ATTN_BUTTN(ctrl))
1194 1055 cmd |= ATTN_BUTTN_ENABLE;
1195 if (POWER_CTRL(slot_cap)) 1056 if (POWER_CTRL(ctrl))
1196 intr_enable = intr_enable | PWR_FAULT_DETECT_ENABLE; 1057 cmd |= PWR_FAULT_DETECT_ENABLE;
1197 1058 if (MRL_SENS(ctrl))
1198 if (MRL_SENS(slot_cap)) 1059 cmd |= MRL_DETECT_ENABLE;
1199 intr_enable = intr_enable | MRL_DETECT_ENABLE; 1060 if (!pciehp_poll_mode)
1061 cmd |= HP_INTR_ENABLE;
1200 1062
1201 temp_word = (temp_word & ~intr_enable) | intr_enable; 1063 mask = PRSN_DETECT_ENABLE | ATTN_BUTTN_ENABLE |
1064 PWR_FAULT_DETECT_ENABLE | MRL_DETECT_ENABLE | HP_INTR_ENABLE;
1202 1065
1203 if (pciehp_poll_mode) { 1066 if (pcie_write_cmd(ctrl, cmd, mask)) {
1204 temp_word = (temp_word & ~HP_INTR_ENABLE) | 0x0; 1067 err("%s: Cannot enable software notification\n", __func__);
1205 } else {
1206 temp_word = (temp_word & ~HP_INTR_ENABLE) | HP_INTR_ENABLE;
1207 }
1208
1209 /*
1210 * Unmask Hot-plug Interrupt Enable for the interrupt
1211 * notification mechanism case.
1212 */
1213 rc = pciehp_writew(ctrl, SLOTCTRL, temp_word);
1214 if (rc) {
1215 err("%s: Cannot write to SLOTCTRL register\n", __func__);
1216 goto abort; 1068 goto abort;
1217 } 1069 }
1218 rc = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
1219 if (rc) {
1220 err("%s: Cannot read SLOTSTATUS register\n", __func__);
1221 goto abort_disable_intr;
1222 }
1223
1224 temp_word = 0x1F; /* Clear all events */
1225 rc = pciehp_writew(ctrl, SLOTSTATUS, temp_word);
1226 if (rc) {
1227 err("%s: Cannot write to SLOTSTATUS register\n", __func__);
1228 goto abort_disable_intr;
1229 }
1230 1070
1231 if (pciehp_force) { 1071 if (pciehp_force)
1232 dbg("Bypassing BIOS check for pciehp use on %s\n", 1072 dbg("Bypassing BIOS check for pciehp use on %s\n",
1233 pci_name(ctrl->pci_dev)); 1073 pci_name(ctrl->pci_dev));
1234 } else { 1074 else if (pciehp_get_hp_hw_control_from_firmware(ctrl->pci_dev))
1235 rc = pciehp_get_hp_hw_control_from_firmware(ctrl->pci_dev); 1075 goto abort_disable_intr;
1236 if (rc)
1237 goto abort_disable_intr;
1238 }
1239 1076
1240 return 0; 1077 return 0;
1241 1078
1242 /* We end up here for the many possible ways to fail this API. */ 1079 /* We end up here for the many possible ways to fail this API. */
1243abort_disable_intr: 1080abort_disable_intr:
1244 rc = pciehp_readw(ctrl, SLOTCTRL, &temp_word); 1081 if (pcie_write_cmd(ctrl, 0, HP_INTR_ENABLE))
1245 if (!rc) {
1246 temp_word &= ~(intr_enable | HP_INTR_ENABLE);
1247 rc = pciehp_writew(ctrl, SLOTCTRL, temp_word);
1248 }
1249 if (rc)
1250 err("%s : disabling interrupts failed\n", __func__); 1082 err("%s : disabling interrupts failed\n", __func__);
1251abort: 1083abort:
1252 return -1; 1084 return -1;
1253} 1085}
1254 1086
1255int pcie_init(struct controller *ctrl, struct pcie_device *dev) 1087static inline void dbg_ctrl(struct controller *ctrl)
1256{ 1088{
1257 int rc; 1089 int i;
1258 u16 cap_reg; 1090 u16 reg16;
1259 u32 slot_cap; 1091 struct pci_dev *pdev = ctrl->pci_dev;
1260 int cap_base;
1261 u16 slot_status, slot_ctrl;
1262 struct pci_dev *pdev;
1263
1264 pdev = dev->port;
1265 ctrl->pci_dev = pdev; /* save pci_dev in context */
1266
1267 dbg("%s: hotplug controller vendor id 0x%x device id 0x%x\n",
1268 __func__, pdev->vendor, pdev->device);
1269 1092
1270 cap_base = pci_find_capability(pdev, PCI_CAP_ID_EXP); 1093 if (!pciehp_debug)
1271 if (cap_base == 0) { 1094 return;
1272 dbg("%s: Can't find PCI_CAP_ID_EXP (0x10)\n", __func__);
1273 goto abort;
1274 }
1275 1095
1276 ctrl->cap_base = cap_base; 1096 dbg("Hotplug Controller:\n");
1097 dbg(" Seg/Bus/Dev/Func/IRQ : %s IRQ %d\n", pci_name(pdev), pdev->irq);
1098 dbg(" Vendor ID : 0x%04x\n", pdev->vendor);
1099 dbg(" Device ID : 0x%04x\n", pdev->device);
1100 dbg(" Subsystem ID : 0x%04x\n", pdev->subsystem_device);
1101 dbg(" Subsystem Vendor ID : 0x%04x\n", pdev->subsystem_vendor);
1102 dbg(" PCIe Cap offset : 0x%02x\n", ctrl->cap_base);
1103 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1104 if (!pci_resource_len(pdev, i))
1105 continue;
1106 dbg(" PCI resource [%d] : 0x%llx@0x%llx\n", i,
1107 (unsigned long long)pci_resource_len(pdev, i),
1108 (unsigned long long)pci_resource_start(pdev, i));
1109 }
1110 dbg("Slot Capabilities : 0x%08x\n", ctrl->slot_cap);
1111 dbg(" Physical Slot Number : %d\n", ctrl->first_slot);
1112 dbg(" Attention Button : %3s\n", ATTN_BUTTN(ctrl) ? "yes" : "no");
1113 dbg(" Power Controller : %3s\n", POWER_CTRL(ctrl) ? "yes" : "no");
1114 dbg(" MRL Sensor : %3s\n", MRL_SENS(ctrl) ? "yes" : "no");
1115 dbg(" Attention Indicator : %3s\n", ATTN_LED(ctrl) ? "yes" : "no");
1116 dbg(" Power Indicator : %3s\n", PWR_LED(ctrl) ? "yes" : "no");
1117 dbg(" Hot-Plug Surprise : %3s\n", HP_SUPR_RM(ctrl) ? "yes" : "no");
1118 dbg(" EMI Present : %3s\n", EMI(ctrl) ? "yes" : "no");
1119 pciehp_readw(ctrl, SLOTSTATUS, &reg16);
1120 dbg("Slot Status : 0x%04x\n", reg16);
1121 pciehp_readw(ctrl, SLOTSTATUS, &reg16);
1122 dbg("Slot Control : 0x%04x\n", reg16);
1123}
1277 1124
1278 dbg("%s: pcie_cap_base %x\n", __func__, cap_base); 1125int pcie_init(struct controller *ctrl, struct pcie_device *dev)
1126{
1127 u32 slot_cap;
1128 struct pci_dev *pdev = dev->port;
1279 1129
1280 rc = pciehp_readw(ctrl, CAPREG, &cap_reg); 1130 ctrl->pci_dev = pdev;
1281 if (rc) { 1131 ctrl->cap_base = pci_find_capability(pdev, PCI_CAP_ID_EXP);
1282 err("%s: Cannot read CAPREG register\n", __func__); 1132 if (!ctrl->cap_base) {
1283 goto abort; 1133 err("%s: Cannot find PCI Express capability\n", __func__);
1284 }
1285 dbg("%s: CAPREG offset %x cap_reg %x\n",
1286 __func__, ctrl->cap_base + CAPREG, cap_reg);
1287
1288 if (((cap_reg & SLOT_IMPL) == 0) ||
1289 (((cap_reg & DEV_PORT_TYPE) != 0x0040)
1290 && ((cap_reg & DEV_PORT_TYPE) != 0x0060))) {
1291 dbg("%s : This is not a root port or the port is not "
1292 "connected to a slot\n", __func__);
1293 goto abort; 1134 goto abort;
1294 } 1135 }
1295 1136 if (pciehp_readl(ctrl, SLOTCAP, &slot_cap)) {
1296 rc = pciehp_readl(ctrl, SLOTCAP, &slot_cap);
1297 if (rc) {
1298 err("%s: Cannot read SLOTCAP register\n", __func__); 1137 err("%s: Cannot read SLOTCAP register\n", __func__);
1299 goto abort; 1138 goto abort;
1300 } 1139 }
1301 dbg("%s: SLOTCAP offset %x slot_cap %x\n",
1302 __func__, ctrl->cap_base + SLOTCAP, slot_cap);
1303
1304 if (!(slot_cap & HP_CAP)) {
1305 dbg("%s : This slot is not hot-plug capable\n", __func__);
1306 goto abort;
1307 }
1308 /* For debugging purpose */
1309 rc = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
1310 if (rc) {
1311 err("%s: Cannot read SLOTSTATUS register\n", __func__);
1312 goto abort;
1313 }
1314 dbg("%s: SLOTSTATUS offset %x slot_status %x\n",
1315 __func__, ctrl->cap_base + SLOTSTATUS, slot_status);
1316
1317 rc = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
1318 if (rc) {
1319 err("%s: Cannot read SLOTCTRL register\n", __func__);
1320 goto abort;
1321 }
1322 dbg("%s: SLOTCTRL offset %x slot_ctrl %x\n",
1323 __func__, ctrl->cap_base + SLOTCTRL, slot_ctrl);
1324
1325 for (rc = 0; rc < DEVICE_COUNT_RESOURCE; rc++)
1326 if (pci_resource_len(pdev, rc) > 0)
1327 dbg("pci resource[%d] start=0x%llx(len=0x%llx)\n", rc,
1328 (unsigned long long)pci_resource_start(pdev, rc),
1329 (unsigned long long)pci_resource_len(pdev, rc));
1330
1331 info("HPC vendor_id %x device_id %x ss_vid %x ss_did %x\n",
1332 pdev->vendor, pdev->device,
1333 pdev->subsystem_vendor, pdev->subsystem_device);
1334 1140
1141 ctrl->slot_cap = slot_cap;
1142 ctrl->first_slot = slot_cap >> 19;
1143 ctrl->slot_device_offset = 0;
1144 ctrl->num_slots = 1;
1145 ctrl->hpc_ops = &pciehp_hpc_ops;
1335 mutex_init(&ctrl->crit_sect); 1146 mutex_init(&ctrl->crit_sect);
1336 mutex_init(&ctrl->ctrl_lock); 1147 mutex_init(&ctrl->ctrl_lock);
1337 spin_lock_init(&ctrl->lock);
1338
1339 /* setup wait queue */
1340 init_waitqueue_head(&ctrl->queue); 1148 init_waitqueue_head(&ctrl->queue);
1149 dbg_ctrl(ctrl);
1341 1150
1342 /* return PCI Controller Info */ 1151 info("HPC vendor_id %x device_id %x ss_vid %x ss_did %x\n",
1343 ctrl->slot_device_offset = 0; 1152 pdev->vendor, pdev->device,
1344 ctrl->num_slots = 1; 1153 pdev->subsystem_vendor, pdev->subsystem_device);
1345 ctrl->first_slot = slot_cap >> 19;
1346 ctrl->ctrlcap = slot_cap & 0x0000007f;
1347 1154
1348 rc = pcie_init_hardware_part1(ctrl, dev); 1155 if (pcie_init_hardware_part1(ctrl, dev))
1349 if (rc)
1350 goto abort; 1156 goto abort;
1351 1157
1352 if (pciehp_poll_mode) { 1158 if (pciehp_request_irq(ctrl))
1353 /* Install interrupt polling timer. Start with 10 sec delay */ 1159 goto abort;
1354 init_timer(&ctrl->poll_timer);
1355 start_int_poll_timer(ctrl, 10);
1356 } else {
1357 /* Installs the interrupt handler */
1358 rc = request_irq(ctrl->pci_dev->irq, pcie_isr, IRQF_SHARED,
1359 MY_NAME, (void *)ctrl);
1360 dbg("%s: request_irq %d for hpc%d (returns %d)\n",
1361 __func__, ctrl->pci_dev->irq,
1362 atomic_read(&pciehp_num_controllers), rc);
1363 if (rc) {
1364 err("Can't get irq %d for the hotplug controller\n",
1365 ctrl->pci_dev->irq);
1366 goto abort;
1367 }
1368 }
1369 dbg("pciehp ctrl b:d:f:irq=0x%x:%x:%x:%x\n", pdev->bus->number,
1370 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn), dev->irq);
1371 1160
1372 /* 1161 /*
1373 * If this is the first controller to be initialized, 1162 * If this is the first controller to be initialized,
@@ -1376,21 +1165,17 @@ int pcie_init(struct controller *ctrl, struct pcie_device *dev)
1376 if (atomic_add_return(1, &pciehp_num_controllers) == 1) { 1165 if (atomic_add_return(1, &pciehp_num_controllers) == 1) {
1377 pciehp_wq = create_singlethread_workqueue("pciehpd"); 1166 pciehp_wq = create_singlethread_workqueue("pciehpd");
1378 if (!pciehp_wq) { 1167 if (!pciehp_wq) {
1379 rc = -ENOMEM;
1380 goto abort_free_irq; 1168 goto abort_free_irq;
1381 } 1169 }
1382 } 1170 }
1383 1171
1384 rc = pcie_init_hardware_part2(ctrl, dev); 1172 if (pcie_init_hardware_part2(ctrl, dev))
1385 if (rc == 0) { 1173 goto abort_free_irq;
1386 ctrl->hpc_ops = &pciehp_hpc_ops; 1174
1387 return 0; 1175 return 0;
1388 } 1176
1389abort_free_irq: 1177abort_free_irq:
1390 if (pciehp_poll_mode) 1178 pciehp_free_irq(ctrl);
1391 del_timer_sync(&ctrl->poll_timer);
1392 else
1393 free_irq(ctrl->pci_dev->irq, ctrl);
1394abort: 1179abort:
1395 return -1; 1180 return -1;
1396} 1181}