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),
109 static inline int pciehp_readw(struct controller *ctrl, int reg, u16 *value)
111 struct pci_dev *dev = ctrl->pci_dev;
112 return pci_read_config_word(dev, ctrl->cap_base + reg, value);
115 static inline int pciehp_readl(struct controller *ctrl, int reg, u32 *value)
117 struct pci_dev *dev = ctrl->pci_dev;
118 return pci_read_config_dword(dev, ctrl->cap_base + reg, value);
121 static inline int pciehp_writew(struct controller *ctrl, int reg, u16 value)
123 struct pci_dev *dev = ctrl->pci_dev;
124 return pci_write_config_word(dev, ctrl->cap_base + reg, value);
127 static inline int pciehp_writel(struct controller *ctrl, int reg, u32 value)
129 struct pci_dev *dev = ctrl->pci_dev;
130 return pci_write_config_dword(dev, ctrl->cap_base + reg, value);
133 /* Field definitions in PCI Express Capabilities Register */
134 #define CAP_VER 0x000F
135 #define DEV_PORT_TYPE 0x00F0
136 #define SLOT_IMPL 0x0100
137 #define MSG_NUM 0x3E00
139 /* Device or Port Type */
140 #define NAT_ENDPT 0x00
141 #define LEG_ENDPT 0x01
142 #define ROOT_PORT 0x04
143 #define UP_STREAM 0x05
144 #define DN_STREAM 0x06
145 #define PCIE_PCI_BRDG 0x07
146 #define PCI_PCIE_BRDG 0x10
148 /* Field definitions in Device Capabilities Register */
149 #define DATTN_BUTTN_PRSN 0x1000
150 #define DATTN_LED_PRSN 0x2000
151 #define DPWR_LED_PRSN 0x4000
153 /* Field definitions in Link Capabilities Register */
154 #define MAX_LNK_SPEED 0x000F
155 #define MAX_LNK_WIDTH 0x03F0
157 /* Link Width Encoding */
166 /*Field definitions of Link Status Register */
167 #define LNK_SPEED 0x000F
168 #define NEG_LINK_WD 0x03F0
169 #define LNK_TRN_ERR 0x0400
170 #define LNK_TRN 0x0800
171 #define SLOT_CLK_CONF 0x1000
173 /* Field definitions in Slot Capabilities Register */
174 #define ATTN_BUTTN_PRSN 0x00000001
175 #define PWR_CTRL_PRSN 0x00000002
176 #define MRL_SENS_PRSN 0x00000004
177 #define ATTN_LED_PRSN 0x00000008
178 #define PWR_LED_PRSN 0x00000010
179 #define HP_SUPR_RM_SUP 0x00000020
180 #define HP_CAP 0x00000040
181 #define SLOT_PWR_VALUE 0x000003F8
182 #define SLOT_PWR_LIMIT 0x00000C00
183 #define PSN 0xFFF80000 /* PSN: Physical Slot Number */
185 /* Field definitions in Slot Control Register */
186 #define ATTN_BUTTN_ENABLE 0x0001
187 #define PWR_FAULT_DETECT_ENABLE 0x0002
188 #define MRL_DETECT_ENABLE 0x0004
189 #define PRSN_DETECT_ENABLE 0x0008
190 #define CMD_CMPL_INTR_ENABLE 0x0010
191 #define HP_INTR_ENABLE 0x0020
192 #define ATTN_LED_CTRL 0x00C0
193 #define PWR_LED_CTRL 0x0300
194 #define PWR_CTRL 0x0400
196 /* Attention indicator and Power indicator states */
198 #define LED_BLINK 0x10
201 /* Power Control Command */
203 #define POWER_OFF 0x0400
205 /* Field definitions in Slot Status Register */
206 #define ATTN_BUTTN_PRESSED 0x0001
207 #define PWR_FAULT_DETECTED 0x0002
208 #define MRL_SENS_CHANGED 0x0004
209 #define PRSN_DETECT_CHANGED 0x0008
210 #define CMD_COMPLETED 0x0010
211 #define MRL_STATE 0x0020
212 #define PRSN_STATE 0x0040
214 static spinlock_t hpc_event_lock;
216 DEFINE_DBG_BUFFER /* Debug string buffer for entire HPC defined here */
217 static int ctlr_seq_num = 0; /* Controller sequence # */
219 static irqreturn_t pcie_isr(int irq, void *dev_id);
220 static void start_int_poll_timer(struct controller *ctrl, int sec);
222 /* This is the interrupt polling timeout function. */
223 static void int_poll_timeout(unsigned long data)
225 struct controller *ctrl = (struct controller *)data;
229 /* Poll for interrupt events. regs == NULL => polling */
232 init_timer(&ctrl->poll_timer);
233 if (!pciehp_poll_time)
234 pciehp_poll_time = 2; /* reset timer to poll in 2 secs if user doesn't specify at module installation*/
236 start_int_poll_timer(ctrl, pciehp_poll_time);
239 /* This function starts the interrupt polling timer. */
240 static void start_int_poll_timer(struct controller *ctrl, int sec)
242 /* Clamp to sane value */
243 if ((sec <= 0) || (sec > 60))
246 ctrl->poll_timer.function = &int_poll_timeout;
247 ctrl->poll_timer.data = (unsigned long)ctrl;
248 ctrl->poll_timer.expires = jiffies + sec * HZ;
249 add_timer(&ctrl->poll_timer);
252 static inline int pcie_wait_cmd(struct controller *ctrl)
255 unsigned int msecs = pciehp_poll_mode ? 2500 : 1000;
256 unsigned long timeout = msecs_to_jiffies(msecs);
259 rc = wait_event_interruptible_timeout(ctrl->queue,
260 !ctrl->cmd_busy, timeout);
262 dbg("Command not completed in 1000 msec\n");
265 info("Command was interrupted by a signal\n");
271 static int pcie_write_cmd(struct slot *slot, u16 cmd)
273 struct controller *ctrl = slot->ctrl;
279 mutex_lock(&ctrl->ctrl_lock);
281 retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
283 err("%s: Cannot read SLOTSTATUS register\n", __FUNCTION__);
287 if ((slot_status & CMD_COMPLETED) == CMD_COMPLETED ) {
288 /* After 1 sec and CMD_COMPLETED still not set, just
289 proceed forward to issue the next command according
290 to spec. Just print out the error message */
291 dbg("%s: CMD_COMPLETED not clear after 1 sec.\n",
296 retval = pciehp_writew(ctrl, SLOTCTRL, (cmd | CMD_CMPL_INTR_ENABLE));
298 err("%s: Cannot write to SLOTCTRL register\n", __FUNCTION__);
303 * Wait for command completion.
305 retval = pcie_wait_cmd(ctrl);
307 mutex_unlock(&ctrl->ctrl_lock);
312 static int hpc_check_lnk_status(struct controller *ctrl)
319 retval = pciehp_readw(ctrl, LNKSTATUS, &lnk_status);
321 err("%s: Cannot read LNKSTATUS register\n", __FUNCTION__);
325 dbg("%s: lnk_status = %x\n", __FUNCTION__, lnk_status);
326 if ( (lnk_status & LNK_TRN) || (lnk_status & LNK_TRN_ERR) ||
327 !(lnk_status & NEG_LINK_WD)) {
328 err("%s : Link Training Error occurs \n", __FUNCTION__);
338 static int hpc_get_attention_status(struct slot *slot, u8 *status)
340 struct controller *ctrl = slot->ctrl;
347 retval = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
349 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
353 dbg("%s: SLOTCTRL %x, value read %x\n",
354 __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_ctrl);
356 atten_led_state = (slot_ctrl & ATTN_LED_CTRL) >> 6;
358 switch (atten_led_state) {
360 *status = 0xFF; /* Reserved */
363 *status = 1; /* On */
366 *status = 2; /* Blink */
369 *status = 0; /* Off */
380 static int hpc_get_power_status(struct slot *slot, u8 *status)
382 struct controller *ctrl = slot->ctrl;
389 retval = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
391 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
394 dbg("%s: SLOTCTRL %x value read %x\n",
395 __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_ctrl);
397 pwr_state = (slot_ctrl & PWR_CTRL) >> 10;
416 static int hpc_get_latch_status(struct slot *slot, u8 *status)
418 struct controller *ctrl = slot->ctrl;
424 retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
426 err("%s: Cannot read SLOTSTATUS register\n", __FUNCTION__);
430 *status = (((slot_status & MRL_STATE) >> 5) == 0) ? 0 : 1;
436 static int hpc_get_adapter_status(struct slot *slot, u8 *status)
438 struct controller *ctrl = slot->ctrl;
445 retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
447 err("%s: Cannot read SLOTSTATUS register\n", __FUNCTION__);
450 card_state = (u8)((slot_status & PRSN_STATE) >> 6);
451 *status = (card_state == 1) ? 1 : 0;
457 static int hpc_query_power_fault(struct slot *slot)
459 struct controller *ctrl = slot->ctrl;
466 retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
468 err("%s: Cannot check for power fault\n", __FUNCTION__);
471 pwr_fault = (u8)((slot_status & PWR_FAULT_DETECTED) >> 1);
477 static int hpc_set_attention_status(struct slot *slot, u8 value)
479 struct controller *ctrl = slot->ctrl;
486 rc = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
488 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
493 case 0 : /* turn off */
494 slot_cmd = (slot_ctrl & ~ATTN_LED_CTRL) | 0x00C0;
496 case 1: /* turn on */
497 slot_cmd = (slot_ctrl & ~ATTN_LED_CTRL) | 0x0040;
499 case 2: /* turn blink */
500 slot_cmd = (slot_ctrl & ~ATTN_LED_CTRL) | 0x0080;
505 if (!pciehp_poll_mode)
506 slot_cmd = slot_cmd | HP_INTR_ENABLE;
508 pcie_write_cmd(slot, slot_cmd);
509 dbg("%s: SLOTCTRL %x write cmd %x\n",
510 __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_cmd);
517 static void hpc_set_green_led_on(struct slot *slot)
519 struct controller *ctrl = slot->ctrl;
526 rc = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
528 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
531 slot_cmd = (slot_ctrl & ~PWR_LED_CTRL) | 0x0100;
532 if (!pciehp_poll_mode)
533 slot_cmd = slot_cmd | HP_INTR_ENABLE;
535 pcie_write_cmd(slot, slot_cmd);
537 dbg("%s: SLOTCTRL %x write cmd %x\n",
538 __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_cmd);
543 static void hpc_set_green_led_off(struct slot *slot)
545 struct controller *ctrl = slot->ctrl;
552 rc = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
554 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
558 slot_cmd = (slot_ctrl & ~PWR_LED_CTRL) | 0x0300;
560 if (!pciehp_poll_mode)
561 slot_cmd = slot_cmd | HP_INTR_ENABLE;
562 pcie_write_cmd(slot, slot_cmd);
563 dbg("%s: SLOTCTRL %x write cmd %x\n",
564 __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_cmd);
570 static void hpc_set_green_led_blink(struct slot *slot)
572 struct controller *ctrl = slot->ctrl;
579 rc = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
581 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
585 slot_cmd = (slot_ctrl & ~PWR_LED_CTRL) | 0x0200;
587 if (!pciehp_poll_mode)
588 slot_cmd = slot_cmd | HP_INTR_ENABLE;
589 pcie_write_cmd(slot, slot_cmd);
591 dbg("%s: SLOTCTRL %x write cmd %x\n",
592 __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_cmd);
597 static void hpc_release_ctlr(struct controller *ctrl)
601 if (pciehp_poll_mode)
602 del_timer(&ctrl->poll_timer);
604 free_irq(ctrl->pci_dev->irq, ctrl);
609 static int hpc_power_on_slot(struct slot * slot)
611 struct controller *ctrl = slot->ctrl;
613 u16 slot_ctrl, slot_status;
618 dbg("%s: slot->hp_slot %x\n", __FUNCTION__, slot->hp_slot);
620 /* Clear sticky power-fault bit from previous power failures */
621 retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
623 err("%s: Cannot read SLOTSTATUS register\n", __FUNCTION__);
626 slot_status &= PWR_FAULT_DETECTED;
628 retval = pciehp_writew(ctrl, SLOTSTATUS, slot_status);
630 err("%s: Cannot write to SLOTSTATUS register\n",
636 retval = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
638 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
642 slot_cmd = (slot_ctrl & ~PWR_CTRL) | POWER_ON;
644 /* Enable detection that we turned off at slot power-off time */
645 if (!pciehp_poll_mode)
646 slot_cmd = slot_cmd |
647 PWR_FAULT_DETECT_ENABLE |
652 retval = pcie_write_cmd(slot, slot_cmd);
655 err("%s: Write %x command failed!\n", __FUNCTION__, slot_cmd);
658 dbg("%s: SLOTCTRL %x write cmd %x\n",
659 __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_cmd);
666 static int hpc_power_off_slot(struct slot * slot)
668 struct controller *ctrl = slot->ctrl;
675 dbg("%s: slot->hp_slot %x\n", __FUNCTION__, slot->hp_slot);
677 retval = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
679 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
683 slot_cmd = (slot_ctrl & ~PWR_CTRL) | POWER_OFF;
686 * If we get MRL or presence detect interrupts now, the isr
687 * will notice the sticky power-fault bit too and issue power
688 * indicator change commands. This will lead to an endless loop
689 * of command completions, since the power-fault bit remains on
690 * till the slot is powered on again.
692 if (!pciehp_poll_mode)
693 slot_cmd = (slot_cmd &
694 ~PWR_FAULT_DETECT_ENABLE &
696 ~PRSN_DETECT_ENABLE) | HP_INTR_ENABLE;
698 retval = pcie_write_cmd(slot, slot_cmd);
701 err("%s: Write command failed!\n", __FUNCTION__);
704 dbg("%s: SLOTCTRL %x write cmd %x\n",
705 __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_cmd);
712 static irqreturn_t pcie_isr(int irq, void *dev_id)
714 struct controller *ctrl = (struct controller *)dev_id;
715 u16 slot_status, intr_detect, intr_loc;
717 int hp_slot = 0; /* only 1 slot per PCI Express port */
720 rc = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
722 err("%s: Cannot read SLOTSTATUS register\n", __FUNCTION__);
726 intr_detect = ( ATTN_BUTTN_PRESSED | PWR_FAULT_DETECTED | MRL_SENS_CHANGED |
727 PRSN_DETECT_CHANGED | CMD_COMPLETED );
729 intr_loc = slot_status & intr_detect;
731 /* Check to see if it was our interrupt */
735 dbg("%s: intr_loc %x\n", __FUNCTION__, intr_loc);
736 /* Mask Hot-plug Interrupt Enable */
737 if (!pciehp_poll_mode) {
738 rc = pciehp_readw(ctrl, SLOTCTRL, &temp_word);
740 err("%s: Cannot read SLOT_CTRL register\n",
745 dbg("%s: pciehp_readw(SLOTCTRL) with value %x\n",
746 __FUNCTION__, temp_word);
747 temp_word = (temp_word & ~HP_INTR_ENABLE & ~CMD_CMPL_INTR_ENABLE) | 0x00;
748 rc = pciehp_writew(ctrl, SLOTCTRL, temp_word);
750 err("%s: Cannot write to SLOTCTRL register\n",
755 rc = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
757 err("%s: Cannot read SLOT_STATUS register\n",
761 dbg("%s: pciehp_readw(SLOTSTATUS) with value %x\n",
762 __FUNCTION__, slot_status);
764 /* Clear command complete interrupt caused by this write */
766 rc = pciehp_writew(ctrl, SLOTSTATUS, temp_word);
768 err("%s: Cannot write to SLOTSTATUS register\n",
774 if (intr_loc & CMD_COMPLETED) {
776 * Command Complete Interrupt Pending
779 wake_up_interruptible(&ctrl->queue);
782 if (intr_loc & MRL_SENS_CHANGED)
783 pciehp_handle_switch_change(hp_slot, ctrl);
785 if (intr_loc & ATTN_BUTTN_PRESSED)
786 pciehp_handle_attention_button(hp_slot, ctrl);
788 if (intr_loc & PRSN_DETECT_CHANGED)
789 pciehp_handle_presence_change(hp_slot, ctrl);
791 if (intr_loc & PWR_FAULT_DETECTED)
792 pciehp_handle_power_fault(hp_slot, ctrl);
794 /* Clear all events after serving them */
796 rc = pciehp_writew(ctrl, SLOTSTATUS, temp_word);
798 err("%s: Cannot write to SLOTSTATUS register\n", __FUNCTION__);
801 /* Unmask Hot-plug Interrupt Enable */
802 if (!pciehp_poll_mode) {
803 rc = pciehp_readw(ctrl, SLOTCTRL, &temp_word);
805 err("%s: Cannot read SLOTCTRL register\n",
810 dbg("%s: Unmask Hot-plug Interrupt Enable\n", __FUNCTION__);
811 temp_word = (temp_word & ~HP_INTR_ENABLE) | HP_INTR_ENABLE;
813 rc = pciehp_writew(ctrl, SLOTCTRL, temp_word);
815 err("%s: Cannot write to SLOTCTRL register\n",
820 rc = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
822 err("%s: Cannot read SLOT_STATUS register\n",
827 /* Clear command complete interrupt caused by this write */
829 rc = pciehp_writew(ctrl, SLOTSTATUS, temp_word);
831 err("%s: Cannot write to SLOTSTATUS failed\n",
835 dbg("%s: pciehp_writew(SLOTSTATUS) with value %x\n",
836 __FUNCTION__, temp_word);
842 static int hpc_get_max_lnk_speed (struct slot *slot, enum pci_bus_speed *value)
844 struct controller *ctrl = slot->ctrl;
845 enum pcie_link_speed lnk_speed;
851 retval = pciehp_readl(ctrl, LNKCAP, &lnk_cap);
853 err("%s: Cannot read LNKCAP register\n", __FUNCTION__);
857 switch (lnk_cap & 0x000F) {
859 lnk_speed = PCIE_2PT5GB;
862 lnk_speed = PCIE_LNK_SPEED_UNKNOWN;
867 dbg("Max link speed = %d\n", lnk_speed);
872 static int hpc_get_max_lnk_width (struct slot *slot, enum pcie_link_width *value)
874 struct controller *ctrl = slot->ctrl;
875 enum pcie_link_width lnk_wdth;
881 retval = pciehp_readl(ctrl, LNKCAP, &lnk_cap);
883 err("%s: Cannot read LNKCAP register\n", __FUNCTION__);
887 switch ((lnk_cap & 0x03F0) >> 4){
889 lnk_wdth = PCIE_LNK_WIDTH_RESRV;
892 lnk_wdth = PCIE_LNK_X1;
895 lnk_wdth = PCIE_LNK_X2;
898 lnk_wdth = PCIE_LNK_X4;
901 lnk_wdth = PCIE_LNK_X8;
904 lnk_wdth = PCIE_LNK_X12;
907 lnk_wdth = PCIE_LNK_X16;
910 lnk_wdth = PCIE_LNK_X32;
913 lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
918 dbg("Max link width = %d\n", lnk_wdth);
923 static int hpc_get_cur_lnk_speed (struct slot *slot, enum pci_bus_speed *value)
925 struct controller *ctrl = slot->ctrl;
926 enum pcie_link_speed lnk_speed = PCI_SPEED_UNKNOWN;
932 retval = pciehp_readw(ctrl, LNKSTATUS, &lnk_status);
934 err("%s: Cannot read LNKSTATUS register\n", __FUNCTION__);
938 switch (lnk_status & 0x0F) {
940 lnk_speed = PCIE_2PT5GB;
943 lnk_speed = PCIE_LNK_SPEED_UNKNOWN;
948 dbg("Current link speed = %d\n", lnk_speed);
953 static int hpc_get_cur_lnk_width (struct slot *slot, enum pcie_link_width *value)
955 struct controller *ctrl = slot->ctrl;
956 enum pcie_link_width lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
962 retval = pciehp_readw(ctrl, LNKSTATUS, &lnk_status);
964 err("%s: Cannot read LNKSTATUS register\n", __FUNCTION__);
968 switch ((lnk_status & 0x03F0) >> 4){
970 lnk_wdth = PCIE_LNK_WIDTH_RESRV;
973 lnk_wdth = PCIE_LNK_X1;
976 lnk_wdth = PCIE_LNK_X2;
979 lnk_wdth = PCIE_LNK_X4;
982 lnk_wdth = PCIE_LNK_X8;
985 lnk_wdth = PCIE_LNK_X12;
988 lnk_wdth = PCIE_LNK_X16;
991 lnk_wdth = PCIE_LNK_X32;
994 lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
999 dbg("Current link width = %d\n", lnk_wdth);
1004 static struct hpc_ops pciehp_hpc_ops = {
1005 .power_on_slot = hpc_power_on_slot,
1006 .power_off_slot = hpc_power_off_slot,
1007 .set_attention_status = hpc_set_attention_status,
1008 .get_power_status = hpc_get_power_status,
1009 .get_attention_status = hpc_get_attention_status,
1010 .get_latch_status = hpc_get_latch_status,
1011 .get_adapter_status = hpc_get_adapter_status,
1013 .get_max_bus_speed = hpc_get_max_lnk_speed,
1014 .get_cur_bus_speed = hpc_get_cur_lnk_speed,
1015 .get_max_lnk_width = hpc_get_max_lnk_width,
1016 .get_cur_lnk_width = hpc_get_cur_lnk_width,
1018 .query_power_fault = hpc_query_power_fault,
1019 .green_led_on = hpc_set_green_led_on,
1020 .green_led_off = hpc_set_green_led_off,
1021 .green_led_blink = hpc_set_green_led_blink,
1023 .release_ctlr = hpc_release_ctlr,
1024 .check_lnk_status = hpc_check_lnk_status,
1028 int pciehp_acpi_get_hp_hw_control_from_firmware(struct pci_dev *dev)
1031 acpi_handle chandle, handle = DEVICE_ACPI_HANDLE(&(dev->dev));
1032 struct pci_dev *pdev = dev;
1033 struct pci_bus *parent;
1034 struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL };
1037 * Per PCI firmware specification, we should run the ACPI _OSC
1038 * method to get control of hotplug hardware before using it.
1039 * If an _OSC is missing, we look for an OSHP to do the same thing.
1040 * To handle different BIOS behavior, we look for _OSC and OSHP
1041 * within the scope of the hotplug controller and its parents, upto
1042 * the host bridge under which this controller exists.
1046 * This hotplug controller was not listed in the ACPI name
1047 * space at all. Try to get acpi handle of parent pci bus.
1049 if (!pdev || !pdev->bus->parent)
1051 parent = pdev->bus->parent;
1052 dbg("Could not find %s in acpi namespace, trying parent\n",
1055 /* Parent must be a host bridge */
1056 handle = acpi_get_pci_rootbridge_handle(
1057 pci_domain_nr(parent),
1060 handle = DEVICE_ACPI_HANDLE(
1061 &(parent->self->dev));
1062 pdev = parent->self;
1066 acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
1067 dbg("Trying to get hotplug control for %s \n",
1068 (char *)string.pointer);
1069 status = pci_osc_control_set(handle,
1070 OSC_PCI_EXPRESS_NATIVE_HP_CONTROL);
1071 if (status == AE_NOT_FOUND)
1072 status = acpi_run_oshp(handle);
1073 if (ACPI_SUCCESS(status)) {
1074 dbg("Gained control for hotplug HW for pci %s (%s)\n",
1075 pci_name(dev), (char *)string.pointer);
1076 kfree(string.pointer);
1079 if (acpi_root_bridge(handle))
1082 status = acpi_get_parent(chandle, &handle);
1083 if (ACPI_FAILURE(status))
1087 err("Cannot get control of hotplug hardware for pci %s\n",
1090 kfree(string.pointer);
1097 int pcie_init(struct controller * ctrl, struct pcie_device *dev)
1100 static int first = 1;
1103 u16 intr_enable = 0;
1106 u16 slot_status, slot_ctrl;
1107 struct pci_dev *pdev;
1112 ctrl->pci_dev = pdev; /* save pci_dev in context */
1114 dbg("%s: hotplug controller vendor id 0x%x device id 0x%x\n",
1115 __FUNCTION__, pdev->vendor, pdev->device);
1117 if ((cap_base = pci_find_capability(pdev, PCI_CAP_ID_EXP)) == 0) {
1118 dbg("%s: Can't find PCI_CAP_ID_EXP (0x10)\n", __FUNCTION__);
1119 goto abort_free_ctlr;
1122 ctrl->cap_base = cap_base;
1124 dbg("%s: pcie_cap_base %x\n", __FUNCTION__, cap_base);
1126 rc = pciehp_readw(ctrl, CAPREG, &cap_reg);
1128 err("%s: Cannot read CAPREG register\n", __FUNCTION__);
1129 goto abort_free_ctlr;
1131 dbg("%s: CAPREG offset %x cap_reg %x\n",
1132 __FUNCTION__, ctrl->cap_base + CAPREG, cap_reg);
1134 if (((cap_reg & SLOT_IMPL) == 0) || (((cap_reg & DEV_PORT_TYPE) != 0x0040)
1135 && ((cap_reg & DEV_PORT_TYPE) != 0x0060))) {
1136 dbg("%s : This is not a root port or the port is not connected to a slot\n", __FUNCTION__);
1137 goto abort_free_ctlr;
1140 rc = pciehp_readl(ctrl, SLOTCAP, &slot_cap);
1142 err("%s: Cannot read SLOTCAP register\n", __FUNCTION__);
1143 goto abort_free_ctlr;
1145 dbg("%s: SLOTCAP offset %x slot_cap %x\n",
1146 __FUNCTION__, ctrl->cap_base + SLOTCAP, slot_cap);
1148 if (!(slot_cap & HP_CAP)) {
1149 dbg("%s : This slot is not hot-plug capable\n", __FUNCTION__);
1150 goto abort_free_ctlr;
1152 /* For debugging purpose */
1153 rc = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
1155 err("%s: Cannot read SLOTSTATUS register\n", __FUNCTION__);
1156 goto abort_free_ctlr;
1158 dbg("%s: SLOTSTATUS offset %x slot_status %x\n",
1159 __FUNCTION__, ctrl->cap_base + SLOTSTATUS, slot_status);
1161 rc = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
1163 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
1164 goto abort_free_ctlr;
1166 dbg("%s: SLOTCTRL offset %x slot_ctrl %x\n",
1167 __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_ctrl);
1170 spin_lock_init(&hpc_event_lock);
1174 for ( rc = 0; rc < DEVICE_COUNT_RESOURCE; rc++)
1175 if (pci_resource_len(pdev, rc) > 0)
1176 dbg("pci resource[%d] start=0x%llx(len=0x%llx)\n", rc,
1177 (unsigned long long)pci_resource_start(pdev, rc),
1178 (unsigned long long)pci_resource_len(pdev, rc));
1180 info("HPC vendor_id %x device_id %x ss_vid %x ss_did %x\n", pdev->vendor, pdev->device,
1181 pdev->subsystem_vendor, pdev->subsystem_device);
1183 mutex_init(&ctrl->crit_sect);
1184 mutex_init(&ctrl->ctrl_lock);
1186 /* setup wait queue */
1187 init_waitqueue_head(&ctrl->queue);
1189 /* return PCI Controller Info */
1190 ctrl->slot_device_offset = 0;
1191 ctrl->num_slots = 1;
1192 ctrl->first_slot = slot_cap >> 19;
1193 ctrl->ctrlcap = slot_cap & 0x0000007f;
1195 /* Mask Hot-plug Interrupt Enable */
1196 rc = pciehp_readw(ctrl, SLOTCTRL, &temp_word);
1198 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
1199 goto abort_free_ctlr;
1202 dbg("%s: SLOTCTRL %x value read %x\n",
1203 __FUNCTION__, ctrl->cap_base + SLOTCTRL, temp_word);
1204 temp_word = (temp_word & ~HP_INTR_ENABLE & ~CMD_CMPL_INTR_ENABLE) | 0x00;
1206 rc = pciehp_writew(ctrl, SLOTCTRL, temp_word);
1208 err("%s: Cannot write to SLOTCTRL register\n", __FUNCTION__);
1209 goto abort_free_ctlr;
1212 rc = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
1214 err("%s: Cannot read SLOTSTATUS register\n", __FUNCTION__);
1215 goto abort_free_ctlr;
1218 temp_word = 0x1F; /* Clear all events */
1219 rc = pciehp_writew(ctrl, SLOTSTATUS, temp_word);
1221 err("%s: Cannot write to SLOTSTATUS register\n", __FUNCTION__);
1222 goto abort_free_ctlr;
1225 if (pciehp_poll_mode) {
1226 /* Install interrupt polling timer. Start with 10 sec delay */
1227 init_timer(&ctrl->poll_timer);
1228 start_int_poll_timer(ctrl, 10);
1230 /* Installs the interrupt handler */
1231 rc = request_irq(ctrl->pci_dev->irq, pcie_isr, IRQF_SHARED,
1232 MY_NAME, (void *)ctrl);
1233 dbg("%s: request_irq %d for hpc%d (returns %d)\n",
1234 __FUNCTION__, ctrl->pci_dev->irq, ctlr_seq_num, rc);
1236 err("Can't get irq %d for the hotplug controller\n",
1237 ctrl->pci_dev->irq);
1238 goto abort_free_ctlr;
1241 dbg("pciehp ctrl b:d:f:irq=0x%x:%x:%x:%x\n", pdev->bus->number,
1242 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn), dev->irq);
1244 rc = pciehp_readw(ctrl, SLOTCTRL, &temp_word);
1246 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
1247 goto abort_free_irq;
1250 intr_enable = intr_enable | PRSN_DETECT_ENABLE;
1252 if (ATTN_BUTTN(slot_cap))
1253 intr_enable = intr_enable | ATTN_BUTTN_ENABLE;
1255 if (POWER_CTRL(slot_cap))
1256 intr_enable = intr_enable | PWR_FAULT_DETECT_ENABLE;
1258 if (MRL_SENS(slot_cap))
1259 intr_enable = intr_enable | MRL_DETECT_ENABLE;
1261 temp_word = (temp_word & ~intr_enable) | intr_enable;
1263 if (pciehp_poll_mode) {
1264 temp_word = (temp_word & ~HP_INTR_ENABLE) | 0x0;
1266 temp_word = (temp_word & ~HP_INTR_ENABLE) | HP_INTR_ENABLE;
1269 /* Unmask Hot-plug Interrupt Enable for the interrupt notification mechanism case */
1270 rc = pciehp_writew(ctrl, SLOTCTRL, temp_word);
1272 err("%s: Cannot write to SLOTCTRL register\n", __FUNCTION__);
1273 goto abort_free_irq;
1275 rc = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
1277 err("%s: Cannot read SLOTSTATUS register\n", __FUNCTION__);
1278 goto abort_disable_intr;
1281 temp_word = 0x1F; /* Clear all events */
1282 rc = pciehp_writew(ctrl, SLOTSTATUS, temp_word);
1284 err("%s: Cannot write to SLOTSTATUS register\n", __FUNCTION__);
1285 goto abort_disable_intr;
1289 dbg("Bypassing BIOS check for pciehp use on %s\n",
1290 pci_name(ctrl->pci_dev));
1292 rc = pciehp_get_hp_hw_control_from_firmware(ctrl->pci_dev);
1294 goto abort_disable_intr;
1298 ctrl->hpc_ops = &pciehp_hpc_ops;
1303 /* We end up here for the many possible ways to fail this API. */
1305 rc = pciehp_readw(ctrl, SLOTCTRL, &temp_word);
1307 temp_word &= ~(intr_enable | HP_INTR_ENABLE);
1308 rc = pciehp_writew(ctrl, SLOTCTRL, temp_word);
1311 err("%s : disabling interrupts failed\n", __FUNCTION__);
1314 if (pciehp_poll_mode)
1315 del_timer_sync(&ctrl->poll_timer);
1317 free_irq(ctrl->pci_dev->irq, ctrl);