2 * PCI Express PCI Hot Plug Driver
4 * Copyright (C) 1995,2001 Compaq Computer Corporation
5 * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
6 * Copyright (C) 2001 IBM Corp.
7 * Copyright (C) 2003-2004 Intel Corporation
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or (at
14 * your option) any later version.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
19 * NON INFRINGEMENT. See the GNU General Public License for more
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 * Send feedback to <greg@kroah.com>,<kristen.c.accardi@intel.com>
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/types.h>
33 #include <linux/signal.h>
34 #include <linux/jiffies.h>
35 #include <linux/timer.h>
36 #include <linux/pci.h>
37 #include <linux/interrupt.h>
42 #define DBG_K_TRACE_ENTRY ((unsigned int)0x00000001) /* On function entry */
43 #define DBG_K_TRACE_EXIT ((unsigned int)0x00000002) /* On function exit */
44 #define DBG_K_INFO ((unsigned int)0x00000004) /* Info messages */
45 #define DBG_K_ERROR ((unsigned int)0x00000008) /* Error messages */
46 #define DBG_K_TRACE (DBG_K_TRACE_ENTRY|DBG_K_TRACE_EXIT)
47 #define DBG_K_STANDARD (DBG_K_INFO|DBG_K_ERROR|DBG_K_TRACE)
48 /* Redefine this flagword to set debug level */
49 #define DEBUG_LEVEL DBG_K_STANDARD
51 #define DEFINE_DBG_BUFFER char __dbg_str_buf[256];
53 #define DBG_PRINT( dbg_flags, args... ) \
55 if ( DEBUG_LEVEL & ( dbg_flags ) ) \
58 len = sprintf( __dbg_str_buf, "%s:%d: %s: ", \
59 __FILE__, __LINE__, __FUNCTION__ ); \
60 sprintf( __dbg_str_buf + len, args ); \
61 printk( KERN_NOTICE "%s\n", __dbg_str_buf ); \
65 #define DBG_ENTER_ROUTINE DBG_PRINT (DBG_K_TRACE_ENTRY, "%s", "[Entry]");
66 #define DBG_LEAVE_ROUTINE DBG_PRINT (DBG_K_TRACE_EXIT, "%s", "[Exit]");
68 #define DEFINE_DBG_BUFFER
69 #define DBG_ENTER_ROUTINE
70 #define DBG_LEAVE_ROUTINE
89 } __attribute__ ((packed));
91 /* offsets to the controller registers based on the above structure layout */
93 PCIECAPID = offsetof(struct ctrl_reg, cap_id),
94 NXTCAPPTR = offsetof(struct ctrl_reg, nxt_ptr),
95 CAPREG = offsetof(struct ctrl_reg, cap_reg),
96 DEVCAP = offsetof(struct ctrl_reg, dev_cap),
97 DEVCTRL = offsetof(struct ctrl_reg, dev_ctrl),
98 DEVSTATUS = offsetof(struct ctrl_reg, dev_status),
99 LNKCAP = offsetof(struct ctrl_reg, lnk_cap),
100 LNKCTRL = offsetof(struct ctrl_reg, lnk_ctrl),
101 LNKSTATUS = offsetof(struct ctrl_reg, lnk_status),
102 SLOTCAP = offsetof(struct ctrl_reg, slot_cap),
103 SLOTCTRL = offsetof(struct ctrl_reg, slot_ctrl),
104 SLOTSTATUS = offsetof(struct ctrl_reg, slot_status),
105 ROOTCTRL = offsetof(struct ctrl_reg, root_ctrl),
106 ROOTSTATUS = offsetof(struct ctrl_reg, root_status),
108 static int pcie_cap_base = 0; /* Base of the PCI Express capability item structure */
110 static inline int pciehp_readw(struct controller *ctrl, int reg, u16 *value)
112 struct pci_dev *dev = ctrl->pci_dev;
113 return pci_read_config_word(dev, ctrl->cap_base + reg, value);
116 static inline int pciehp_readl(struct controller *ctrl, int reg, u32 *value)
118 struct pci_dev *dev = ctrl->pci_dev;
119 return pci_read_config_dword(dev, ctrl->cap_base + reg, value);
122 static inline int pciehp_writew(struct controller *ctrl, int reg, u16 value)
124 struct pci_dev *dev = ctrl->pci_dev;
125 return pci_write_config_word(dev, ctrl->cap_base + reg, value);
128 static inline int pciehp_writel(struct controller *ctrl, int reg, u32 value)
130 struct pci_dev *dev = ctrl->pci_dev;
131 return pci_write_config_dword(dev, ctrl->cap_base + reg, value);
134 /* Field definitions in PCI Express Capabilities Register */
135 #define CAP_VER 0x000F
136 #define DEV_PORT_TYPE 0x00F0
137 #define SLOT_IMPL 0x0100
138 #define MSG_NUM 0x3E00
140 /* Device or Port Type */
141 #define NAT_ENDPT 0x00
142 #define LEG_ENDPT 0x01
143 #define ROOT_PORT 0x04
144 #define UP_STREAM 0x05
145 #define DN_STREAM 0x06
146 #define PCIE_PCI_BRDG 0x07
147 #define PCI_PCIE_BRDG 0x10
149 /* Field definitions in Device Capabilities Register */
150 #define DATTN_BUTTN_PRSN 0x1000
151 #define DATTN_LED_PRSN 0x2000
152 #define DPWR_LED_PRSN 0x4000
154 /* Field definitions in Link Capabilities Register */
155 #define MAX_LNK_SPEED 0x000F
156 #define MAX_LNK_WIDTH 0x03F0
158 /* Link Width Encoding */
167 /*Field definitions of Link Status Register */
168 #define LNK_SPEED 0x000F
169 #define NEG_LINK_WD 0x03F0
170 #define LNK_TRN_ERR 0x0400
171 #define LNK_TRN 0x0800
172 #define SLOT_CLK_CONF 0x1000
174 /* Field definitions in Slot Capabilities Register */
175 #define ATTN_BUTTN_PRSN 0x00000001
176 #define PWR_CTRL_PRSN 0x00000002
177 #define MRL_SENS_PRSN 0x00000004
178 #define ATTN_LED_PRSN 0x00000008
179 #define PWR_LED_PRSN 0x00000010
180 #define HP_SUPR_RM_SUP 0x00000020
181 #define HP_CAP 0x00000040
182 #define SLOT_PWR_VALUE 0x000003F8
183 #define SLOT_PWR_LIMIT 0x00000C00
184 #define PSN 0xFFF80000 /* PSN: Physical Slot Number */
186 /* Field definitions in Slot Control Register */
187 #define ATTN_BUTTN_ENABLE 0x0001
188 #define PWR_FAULT_DETECT_ENABLE 0x0002
189 #define MRL_DETECT_ENABLE 0x0004
190 #define PRSN_DETECT_ENABLE 0x0008
191 #define CMD_CMPL_INTR_ENABLE 0x0010
192 #define HP_INTR_ENABLE 0x0020
193 #define ATTN_LED_CTRL 0x00C0
194 #define PWR_LED_CTRL 0x0300
195 #define PWR_CTRL 0x0400
197 /* Attention indicator and Power indicator states */
199 #define LED_BLINK 0x10
202 /* Power Control Command */
204 #define POWER_OFF 0x0400
206 /* Field definitions in Slot Status Register */
207 #define ATTN_BUTTN_PRESSED 0x0001
208 #define PWR_FAULT_DETECTED 0x0002
209 #define MRL_SENS_CHANGED 0x0004
210 #define PRSN_DETECT_CHANGED 0x0008
211 #define CMD_COMPLETED 0x0010
212 #define MRL_STATE 0x0020
213 #define PRSN_STATE 0x0040
215 static spinlock_t hpc_event_lock;
217 DEFINE_DBG_BUFFER /* Debug string buffer for entire HPC defined here */
218 static int ctlr_seq_num = 0; /* Controller sequence # */
220 static irqreturn_t pcie_isr(int irq, void *dev_id);
221 static void start_int_poll_timer(struct controller *ctrl, int sec);
223 /* This is the interrupt polling timeout function. */
224 static void int_poll_timeout(unsigned long data)
226 struct controller *ctrl = (struct controller *)data;
230 /* Poll for interrupt events. regs == NULL => polling */
233 init_timer(&ctrl->poll_timer);
234 if (!pciehp_poll_time)
235 pciehp_poll_time = 2; /* reset timer to poll in 2 secs if user doesn't specify at module installation*/
237 start_int_poll_timer(ctrl, pciehp_poll_time);
240 /* This function starts the interrupt polling timer. */
241 static void start_int_poll_timer(struct controller *ctrl, int sec)
243 /* Clamp to sane value */
244 if ((sec <= 0) || (sec > 60))
247 ctrl->poll_timer.function = &int_poll_timeout;
248 ctrl->poll_timer.data = (unsigned long)ctrl;
249 ctrl->poll_timer.expires = jiffies + sec * HZ;
250 add_timer(&ctrl->poll_timer);
253 static int pcie_write_cmd(struct slot *slot, u16 cmd)
255 struct controller *ctrl = slot->ctrl;
261 retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
263 err("%s: Cannot read SLOTSTATUS register\n", __FUNCTION__);
267 if ((slot_status & CMD_COMPLETED) == CMD_COMPLETED ) {
268 /* After 1 sec and CMD_COMPLETED still not set, just proceed forward to issue
269 the next command according to spec. Just print out the error message */
270 dbg("%s : CMD_COMPLETED not clear after 1 sec.\n", __FUNCTION__);
273 retval = pciehp_writew(ctrl, SLOTCTRL, (cmd | CMD_CMPL_INTR_ENABLE));
275 err("%s: Cannot write to SLOTCTRL register\n", __FUNCTION__);
283 static int hpc_check_lnk_status(struct controller *ctrl)
290 retval = pciehp_readw(ctrl, LNKSTATUS, &lnk_status);
292 err("%s: Cannot read LNKSTATUS register\n", __FUNCTION__);
296 dbg("%s: lnk_status = %x\n", __FUNCTION__, lnk_status);
297 if ( (lnk_status & LNK_TRN) || (lnk_status & LNK_TRN_ERR) ||
298 !(lnk_status & NEG_LINK_WD)) {
299 err("%s : Link Training Error occurs \n", __FUNCTION__);
309 static int hpc_get_attention_status(struct slot *slot, u8 *status)
311 struct controller *ctrl = slot->ctrl;
318 retval = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
320 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
324 dbg("%s: SLOTCTRL %x, value read %x\n",
325 __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_ctrl);
327 atten_led_state = (slot_ctrl & ATTN_LED_CTRL) >> 6;
329 switch (atten_led_state) {
331 *status = 0xFF; /* Reserved */
334 *status = 1; /* On */
337 *status = 2; /* Blink */
340 *status = 0; /* Off */
351 static int hpc_get_power_status(struct slot *slot, u8 *status)
353 struct controller *ctrl = slot->ctrl;
360 retval = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
362 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
365 dbg("%s: SLOTCTRL %x value read %x\n",
366 __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_ctrl);
368 pwr_state = (slot_ctrl & PWR_CTRL) >> 10;
387 static int hpc_get_latch_status(struct slot *slot, u8 *status)
389 struct controller *ctrl = slot->ctrl;
395 retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
397 err("%s: Cannot read SLOTSTATUS register\n", __FUNCTION__);
401 *status = (((slot_status & MRL_STATE) >> 5) == 0) ? 0 : 1;
407 static int hpc_get_adapter_status(struct slot *slot, u8 *status)
409 struct controller *ctrl = slot->ctrl;
416 retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
418 err("%s: Cannot read SLOTSTATUS register\n", __FUNCTION__);
421 card_state = (u8)((slot_status & PRSN_STATE) >> 6);
422 *status = (card_state == 1) ? 1 : 0;
428 static int hpc_query_power_fault(struct slot *slot)
430 struct controller *ctrl = slot->ctrl;
437 retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
439 err("%s: Cannot check for power fault\n", __FUNCTION__);
442 pwr_fault = (u8)((slot_status & PWR_FAULT_DETECTED) >> 1);
448 static int hpc_set_attention_status(struct slot *slot, u8 value)
450 struct controller *ctrl = slot->ctrl;
457 rc = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
459 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
464 case 0 : /* turn off */
465 slot_cmd = (slot_ctrl & ~ATTN_LED_CTRL) | 0x00C0;
467 case 1: /* turn on */
468 slot_cmd = (slot_ctrl & ~ATTN_LED_CTRL) | 0x0040;
470 case 2: /* turn blink */
471 slot_cmd = (slot_ctrl & ~ATTN_LED_CTRL) | 0x0080;
476 if (!pciehp_poll_mode)
477 slot_cmd = slot_cmd | HP_INTR_ENABLE;
479 pcie_write_cmd(slot, slot_cmd);
480 dbg("%s: SLOTCTRL %x write cmd %x\n",
481 __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_cmd);
488 static void hpc_set_green_led_on(struct slot *slot)
490 struct controller *ctrl = slot->ctrl;
497 rc = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
499 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
502 slot_cmd = (slot_ctrl & ~PWR_LED_CTRL) | 0x0100;
503 if (!pciehp_poll_mode)
504 slot_cmd = slot_cmd | HP_INTR_ENABLE;
506 pcie_write_cmd(slot, slot_cmd);
508 dbg("%s: SLOTCTRL %x write cmd %x\n",
509 __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_cmd);
514 static void hpc_set_green_led_off(struct slot *slot)
516 struct controller *ctrl = slot->ctrl;
523 rc = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
525 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
529 slot_cmd = (slot_ctrl & ~PWR_LED_CTRL) | 0x0300;
531 if (!pciehp_poll_mode)
532 slot_cmd = slot_cmd | HP_INTR_ENABLE;
533 pcie_write_cmd(slot, slot_cmd);
534 dbg("%s: SLOTCTRL %x write cmd %x\n",
535 __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_cmd);
541 static void hpc_set_green_led_blink(struct slot *slot)
543 struct controller *ctrl = slot->ctrl;
550 rc = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
552 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
556 slot_cmd = (slot_ctrl & ~PWR_LED_CTRL) | 0x0200;
558 if (!pciehp_poll_mode)
559 slot_cmd = slot_cmd | HP_INTR_ENABLE;
560 pcie_write_cmd(slot, slot_cmd);
562 dbg("%s: SLOTCTRL %x write cmd %x\n",
563 __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_cmd);
568 static void hpc_release_ctlr(struct controller *ctrl)
572 if (pciehp_poll_mode)
573 del_timer(&ctrl->poll_timer);
575 free_irq(ctrl->pci_dev->irq, ctrl);
580 static int hpc_power_on_slot(struct slot * slot)
582 struct controller *ctrl = slot->ctrl;
584 u16 slot_ctrl, slot_status;
589 dbg("%s: slot->hp_slot %x\n", __FUNCTION__, slot->hp_slot);
591 /* Clear sticky power-fault bit from previous power failures */
592 retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
594 err("%s: Cannot read SLOTSTATUS register\n", __FUNCTION__);
597 slot_status &= PWR_FAULT_DETECTED;
599 retval = pciehp_writew(ctrl, SLOTSTATUS, slot_status);
601 err("%s: Cannot write to SLOTSTATUS register\n",
607 retval = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
609 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
613 slot_cmd = (slot_ctrl & ~PWR_CTRL) | POWER_ON;
615 /* Enable detection that we turned off at slot power-off time */
616 if (!pciehp_poll_mode)
617 slot_cmd = slot_cmd |
618 PWR_FAULT_DETECT_ENABLE |
623 retval = pcie_write_cmd(slot, slot_cmd);
626 err("%s: Write %x command failed!\n", __FUNCTION__, slot_cmd);
629 dbg("%s: SLOTCTRL %x write cmd %x\n",
630 __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_cmd);
637 static int hpc_power_off_slot(struct slot * slot)
639 struct controller *ctrl = slot->ctrl;
646 dbg("%s: slot->hp_slot %x\n", __FUNCTION__, slot->hp_slot);
648 retval = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
650 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
654 slot_cmd = (slot_ctrl & ~PWR_CTRL) | POWER_OFF;
657 * If we get MRL or presence detect interrupts now, the isr
658 * will notice the sticky power-fault bit too and issue power
659 * indicator change commands. This will lead to an endless loop
660 * of command completions, since the power-fault bit remains on
661 * till the slot is powered on again.
663 if (!pciehp_poll_mode)
664 slot_cmd = (slot_cmd &
665 ~PWR_FAULT_DETECT_ENABLE &
667 ~PRSN_DETECT_ENABLE) | HP_INTR_ENABLE;
669 retval = pcie_write_cmd(slot, slot_cmd);
672 err("%s: Write command failed!\n", __FUNCTION__);
675 dbg("%s: SLOTCTRL %x write cmd %x\n",
676 __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_cmd);
683 static irqreturn_t pcie_isr(int irq, void *dev_id)
685 struct controller *ctrl = (struct controller *)dev_id;
686 u16 slot_status, intr_detect, intr_loc;
688 int hp_slot = 0; /* only 1 slot per PCI Express port */
691 rc = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
693 err("%s: Cannot read SLOTSTATUS register\n", __FUNCTION__);
697 intr_detect = ( ATTN_BUTTN_PRESSED | PWR_FAULT_DETECTED | MRL_SENS_CHANGED |
698 PRSN_DETECT_CHANGED | CMD_COMPLETED );
700 intr_loc = slot_status & intr_detect;
702 /* Check to see if it was our interrupt */
706 dbg("%s: intr_loc %x\n", __FUNCTION__, intr_loc);
707 /* Mask Hot-plug Interrupt Enable */
708 if (!pciehp_poll_mode) {
709 rc = pciehp_readw(ctrl, SLOTCTRL, &temp_word);
711 err("%s: Cannot read SLOT_CTRL register\n",
716 dbg("%s: pciehp_readw(SLOTCTRL) with value %x\n",
717 __FUNCTION__, temp_word);
718 temp_word = (temp_word & ~HP_INTR_ENABLE & ~CMD_CMPL_INTR_ENABLE) | 0x00;
719 rc = pciehp_writew(ctrl, SLOTCTRL, temp_word);
721 err("%s: Cannot write to SLOTCTRL register\n",
726 rc = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
728 err("%s: Cannot read SLOT_STATUS register\n",
732 dbg("%s: pciehp_readw(SLOTSTATUS) with value %x\n",
733 __FUNCTION__, slot_status);
735 /* Clear command complete interrupt caused by this write */
737 rc = pciehp_writew(ctrl, SLOTSTATUS, temp_word);
739 err("%s: Cannot write to SLOTSTATUS register\n",
745 if (intr_loc & CMD_COMPLETED) {
747 * Command Complete Interrupt Pending
749 wake_up_interruptible(&ctrl->queue);
752 if (intr_loc & MRL_SENS_CHANGED)
753 pciehp_handle_switch_change(hp_slot, ctrl);
755 if (intr_loc & ATTN_BUTTN_PRESSED)
756 pciehp_handle_attention_button(hp_slot, ctrl);
758 if (intr_loc & PRSN_DETECT_CHANGED)
759 pciehp_handle_presence_change(hp_slot, ctrl);
761 if (intr_loc & PWR_FAULT_DETECTED)
762 pciehp_handle_power_fault(hp_slot, ctrl);
764 /* Clear all events after serving them */
766 rc = pciehp_writew(ctrl, SLOTSTATUS, temp_word);
768 err("%s: Cannot write to SLOTSTATUS register\n", __FUNCTION__);
771 /* Unmask Hot-plug Interrupt Enable */
772 if (!pciehp_poll_mode) {
773 rc = pciehp_readw(ctrl, SLOTCTRL, &temp_word);
775 err("%s: Cannot read SLOTCTRL register\n",
780 dbg("%s: Unmask Hot-plug Interrupt Enable\n", __FUNCTION__);
781 temp_word = (temp_word & ~HP_INTR_ENABLE) | HP_INTR_ENABLE;
783 rc = pciehp_writew(ctrl, SLOTCTRL, temp_word);
785 err("%s: Cannot write to SLOTCTRL register\n",
790 rc = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
792 err("%s: Cannot read SLOT_STATUS register\n",
797 /* Clear command complete interrupt caused by this write */
799 rc = pciehp_writew(ctrl, SLOTSTATUS, temp_word);
801 err("%s: Cannot write to SLOTSTATUS failed\n",
805 dbg("%s: pciehp_writew(SLOTSTATUS) with value %x\n",
806 __FUNCTION__, temp_word);
812 static int hpc_get_max_lnk_speed (struct slot *slot, enum pci_bus_speed *value)
814 struct controller *ctrl = slot->ctrl;
815 enum pcie_link_speed lnk_speed;
821 retval = pciehp_readl(ctrl, LNKCAP, &lnk_cap);
823 err("%s: Cannot read LNKCAP register\n", __FUNCTION__);
827 switch (lnk_cap & 0x000F) {
829 lnk_speed = PCIE_2PT5GB;
832 lnk_speed = PCIE_LNK_SPEED_UNKNOWN;
837 dbg("Max link speed = %d\n", lnk_speed);
842 static int hpc_get_max_lnk_width (struct slot *slot, enum pcie_link_width *value)
844 struct controller *ctrl = slot->ctrl;
845 enum pcie_link_width lnk_wdth;
851 retval = pciehp_readl(ctrl, LNKCAP, &lnk_cap);
853 err("%s: Cannot read LNKCAP register\n", __FUNCTION__);
857 switch ((lnk_cap & 0x03F0) >> 4){
859 lnk_wdth = PCIE_LNK_WIDTH_RESRV;
862 lnk_wdth = PCIE_LNK_X1;
865 lnk_wdth = PCIE_LNK_X2;
868 lnk_wdth = PCIE_LNK_X4;
871 lnk_wdth = PCIE_LNK_X8;
874 lnk_wdth = PCIE_LNK_X12;
877 lnk_wdth = PCIE_LNK_X16;
880 lnk_wdth = PCIE_LNK_X32;
883 lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
888 dbg("Max link width = %d\n", lnk_wdth);
893 static int hpc_get_cur_lnk_speed (struct slot *slot, enum pci_bus_speed *value)
895 struct controller *ctrl = slot->ctrl;
896 enum pcie_link_speed lnk_speed = PCI_SPEED_UNKNOWN;
902 retval = pciehp_readw(ctrl, LNKSTATUS, &lnk_status);
904 err("%s: Cannot read LNKSTATUS register\n", __FUNCTION__);
908 switch (lnk_status & 0x0F) {
910 lnk_speed = PCIE_2PT5GB;
913 lnk_speed = PCIE_LNK_SPEED_UNKNOWN;
918 dbg("Current link speed = %d\n", lnk_speed);
923 static int hpc_get_cur_lnk_width (struct slot *slot, enum pcie_link_width *value)
925 struct controller *ctrl = slot->ctrl;
926 enum pcie_link_width lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
932 retval = pciehp_readw(ctrl, LNKSTATUS, &lnk_status);
934 err("%s: Cannot read LNKSTATUS register\n", __FUNCTION__);
938 switch ((lnk_status & 0x03F0) >> 4){
940 lnk_wdth = PCIE_LNK_WIDTH_RESRV;
943 lnk_wdth = PCIE_LNK_X1;
946 lnk_wdth = PCIE_LNK_X2;
949 lnk_wdth = PCIE_LNK_X4;
952 lnk_wdth = PCIE_LNK_X8;
955 lnk_wdth = PCIE_LNK_X12;
958 lnk_wdth = PCIE_LNK_X16;
961 lnk_wdth = PCIE_LNK_X32;
964 lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
969 dbg("Current link width = %d\n", lnk_wdth);
974 static struct hpc_ops pciehp_hpc_ops = {
975 .power_on_slot = hpc_power_on_slot,
976 .power_off_slot = hpc_power_off_slot,
977 .set_attention_status = hpc_set_attention_status,
978 .get_power_status = hpc_get_power_status,
979 .get_attention_status = hpc_get_attention_status,
980 .get_latch_status = hpc_get_latch_status,
981 .get_adapter_status = hpc_get_adapter_status,
983 .get_max_bus_speed = hpc_get_max_lnk_speed,
984 .get_cur_bus_speed = hpc_get_cur_lnk_speed,
985 .get_max_lnk_width = hpc_get_max_lnk_width,
986 .get_cur_lnk_width = hpc_get_cur_lnk_width,
988 .query_power_fault = hpc_query_power_fault,
989 .green_led_on = hpc_set_green_led_on,
990 .green_led_off = hpc_set_green_led_off,
991 .green_led_blink = hpc_set_green_led_blink,
993 .release_ctlr = hpc_release_ctlr,
994 .check_lnk_status = hpc_check_lnk_status,
998 int pciehp_acpi_get_hp_hw_control_from_firmware(struct pci_dev *dev)
1001 acpi_handle chandle, handle = DEVICE_ACPI_HANDLE(&(dev->dev));
1002 struct pci_dev *pdev = dev;
1003 struct pci_bus *parent;
1004 struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL };
1007 * Per PCI firmware specification, we should run the ACPI _OSC
1008 * method to get control of hotplug hardware before using it.
1009 * If an _OSC is missing, we look for an OSHP to do the same thing.
1010 * To handle different BIOS behavior, we look for _OSC and OSHP
1011 * within the scope of the hotplug controller and its parents, upto
1012 * the host bridge under which this controller exists.
1016 * This hotplug controller was not listed in the ACPI name
1017 * space at all. Try to get acpi handle of parent pci bus.
1019 if (!pdev || !pdev->bus->parent)
1021 parent = pdev->bus->parent;
1022 dbg("Could not find %s in acpi namespace, trying parent\n",
1025 /* Parent must be a host bridge */
1026 handle = acpi_get_pci_rootbridge_handle(
1027 pci_domain_nr(parent),
1030 handle = DEVICE_ACPI_HANDLE(
1031 &(parent->self->dev));
1032 pdev = parent->self;
1036 acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
1037 dbg("Trying to get hotplug control for %s \n",
1038 (char *)string.pointer);
1039 status = pci_osc_control_set(handle,
1040 OSC_PCI_EXPRESS_NATIVE_HP_CONTROL);
1041 if (status == AE_NOT_FOUND)
1042 status = acpi_run_oshp(handle);
1043 if (ACPI_SUCCESS(status)) {
1044 dbg("Gained control for hotplug HW for pci %s (%s)\n",
1045 pci_name(dev), (char *)string.pointer);
1046 kfree(string.pointer);
1049 if (acpi_root_bridge(handle))
1052 status = acpi_get_parent(chandle, &handle);
1053 if (ACPI_FAILURE(status))
1057 err("Cannot get control of hotplug hardware for pci %s\n",
1060 kfree(string.pointer);
1067 int pcie_init(struct controller * ctrl, struct pcie_device *dev)
1070 static int first = 1;
1073 u16 intr_enable = 0;
1075 int cap_base, saved_cap_base;
1076 u16 slot_status, slot_ctrl;
1077 struct pci_dev *pdev;
1082 ctrl->pci_dev = pdev; /* save pci_dev in context */
1084 dbg("%s: hotplug controller vendor id 0x%x device id 0x%x\n",
1085 __FUNCTION__, pdev->vendor, pdev->device);
1087 saved_cap_base = pcie_cap_base;
1089 if ((cap_base = pci_find_capability(pdev, PCI_CAP_ID_EXP)) == 0) {
1090 dbg("%s: Can't find PCI_CAP_ID_EXP (0x10)\n", __FUNCTION__);
1091 goto abort_free_ctlr;
1094 ctrl->cap_base = cap_base;
1096 dbg("%s: pcie_cap_base %x\n", __FUNCTION__, pcie_cap_base);
1098 rc = pciehp_readw(ctrl, CAPREG, &cap_reg);
1100 err("%s: Cannot read CAPREG register\n", __FUNCTION__);
1101 goto abort_free_ctlr;
1103 dbg("%s: CAPREG offset %x cap_reg %x\n",
1104 __FUNCTION__, ctrl->cap_base + CAPREG, cap_reg);
1106 if (((cap_reg & SLOT_IMPL) == 0) || (((cap_reg & DEV_PORT_TYPE) != 0x0040)
1107 && ((cap_reg & DEV_PORT_TYPE) != 0x0060))) {
1108 dbg("%s : This is not a root port or the port is not connected to a slot\n", __FUNCTION__);
1109 goto abort_free_ctlr;
1112 rc = pciehp_readl(ctrl, SLOTCAP, &slot_cap);
1114 err("%s: Cannot read SLOTCAP register\n", __FUNCTION__);
1115 goto abort_free_ctlr;
1117 dbg("%s: SLOTCAP offset %x slot_cap %x\n",
1118 __FUNCTION__, ctrl->cap_base + SLOTCAP, slot_cap);
1120 if (!(slot_cap & HP_CAP)) {
1121 dbg("%s : This slot is not hot-plug capable\n", __FUNCTION__);
1122 goto abort_free_ctlr;
1124 /* For debugging purpose */
1125 rc = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
1127 err("%s: Cannot read SLOTSTATUS register\n", __FUNCTION__);
1128 goto abort_free_ctlr;
1130 dbg("%s: SLOTSTATUS offset %x slot_status %x\n",
1131 __FUNCTION__, ctrl->cap_base + SLOTSTATUS, slot_status);
1133 rc = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
1135 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
1136 goto abort_free_ctlr;
1138 dbg("%s: SLOTCTRL offset %x slot_ctrl %x\n",
1139 __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_ctrl);
1142 spin_lock_init(&hpc_event_lock);
1146 for ( rc = 0; rc < DEVICE_COUNT_RESOURCE; rc++)
1147 if (pci_resource_len(pdev, rc) > 0)
1148 dbg("pci resource[%d] start=0x%llx(len=0x%llx)\n", rc,
1149 (unsigned long long)pci_resource_start(pdev, rc),
1150 (unsigned long long)pci_resource_len(pdev, rc));
1152 info("HPC vendor_id %x device_id %x ss_vid %x ss_did %x\n", pdev->vendor, pdev->device,
1153 pdev->subsystem_vendor, pdev->subsystem_device);
1155 mutex_init(&ctrl->crit_sect);
1156 mutex_init(&ctrl->ctrl_lock);
1158 /* setup wait queue */
1159 init_waitqueue_head(&ctrl->queue);
1161 /* return PCI Controller Info */
1162 ctrl->slot_device_offset = 0;
1163 ctrl->num_slots = 1;
1164 ctrl->first_slot = slot_cap >> 19;
1165 ctrl->ctrlcap = slot_cap & 0x0000007f;
1167 /* Mask Hot-plug Interrupt Enable */
1168 rc = pciehp_readw(ctrl, SLOTCTRL, &temp_word);
1170 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
1171 goto abort_free_ctlr;
1174 dbg("%s: SLOTCTRL %x value read %x\n",
1175 __FUNCTION__, ctrl->cap_base + SLOTCTRL, temp_word);
1176 temp_word = (temp_word & ~HP_INTR_ENABLE & ~CMD_CMPL_INTR_ENABLE) | 0x00;
1178 rc = pciehp_writew(ctrl, SLOTCTRL, temp_word);
1180 err("%s: Cannot write to SLOTCTRL register\n", __FUNCTION__);
1181 goto abort_free_ctlr;
1184 rc = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
1186 err("%s: Cannot read SLOTSTATUS register\n", __FUNCTION__);
1187 goto abort_free_ctlr;
1190 temp_word = 0x1F; /* Clear all events */
1191 rc = pciehp_writew(ctrl, SLOTSTATUS, temp_word);
1193 err("%s: Cannot write to SLOTSTATUS register\n", __FUNCTION__);
1194 goto abort_free_ctlr;
1197 if (pciehp_poll_mode) {
1198 /* Install interrupt polling timer. Start with 10 sec delay */
1199 init_timer(&ctrl->poll_timer);
1200 start_int_poll_timer(ctrl, 10);
1202 /* Installs the interrupt handler */
1203 rc = request_irq(ctrl->pci_dev->irq, pcie_isr, IRQF_SHARED,
1204 MY_NAME, (void *)ctrl);
1205 dbg("%s: request_irq %d for hpc%d (returns %d)\n",
1206 __FUNCTION__, ctrl->pci_dev->irq, ctlr_seq_num, rc);
1208 err("Can't get irq %d for the hotplug controller\n",
1209 ctrl->pci_dev->irq);
1210 goto abort_free_ctlr;
1213 dbg("pciehp ctrl b:d:f:irq=0x%x:%x:%x:%x\n", pdev->bus->number,
1214 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn), dev->irq);
1216 rc = pciehp_readw(ctrl, SLOTCTRL, &temp_word);
1218 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
1219 goto abort_free_irq;
1222 intr_enable = intr_enable | PRSN_DETECT_ENABLE;
1224 if (ATTN_BUTTN(slot_cap))
1225 intr_enable = intr_enable | ATTN_BUTTN_ENABLE;
1227 if (POWER_CTRL(slot_cap))
1228 intr_enable = intr_enable | PWR_FAULT_DETECT_ENABLE;
1230 if (MRL_SENS(slot_cap))
1231 intr_enable = intr_enable | MRL_DETECT_ENABLE;
1233 temp_word = (temp_word & ~intr_enable) | intr_enable;
1235 if (pciehp_poll_mode) {
1236 temp_word = (temp_word & ~HP_INTR_ENABLE) | 0x0;
1238 temp_word = (temp_word & ~HP_INTR_ENABLE) | HP_INTR_ENABLE;
1241 /* Unmask Hot-plug Interrupt Enable for the interrupt notification mechanism case */
1242 rc = pciehp_writew(ctrl, SLOTCTRL, temp_word);
1244 err("%s: Cannot write to SLOTCTRL register\n", __FUNCTION__);
1245 goto abort_free_irq;
1247 rc = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
1249 err("%s: Cannot read SLOTSTATUS register\n", __FUNCTION__);
1250 goto abort_disable_intr;
1253 temp_word = 0x1F; /* Clear all events */
1254 rc = pciehp_writew(ctrl, SLOTSTATUS, temp_word);
1256 err("%s: Cannot write to SLOTSTATUS register\n", __FUNCTION__);
1257 goto abort_disable_intr;
1261 dbg("Bypassing BIOS check for pciehp use on %s\n",
1262 pci_name(ctrl->pci_dev));
1264 rc = pciehp_get_hp_hw_control_from_firmware(ctrl->pci_dev);
1266 goto abort_disable_intr;
1270 ctrl->hpc_ops = &pciehp_hpc_ops;
1275 /* We end up here for the many possible ways to fail this API. */
1277 rc = pciehp_readw(ctrl, SLOTCTRL, &temp_word);
1279 temp_word &= ~(intr_enable | HP_INTR_ENABLE);
1280 rc = pciehp_writew(ctrl, SLOTCTRL, temp_word);
1283 err("%s : disabling interrupts failed\n", __FUNCTION__);
1286 if (pciehp_poll_mode)
1287 del_timer_sync(&ctrl->poll_timer);
1289 free_irq(ctrl->pci_dev->irq, ctrl);
1292 pcie_cap_base = saved_cap_base;