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>
43 #define DBG_K_TRACE_ENTRY ((unsigned int)0x00000001) /* On function entry */
44 #define DBG_K_TRACE_EXIT ((unsigned int)0x00000002) /* On function exit */
45 #define DBG_K_INFO ((unsigned int)0x00000004) /* Info messages */
46 #define DBG_K_ERROR ((unsigned int)0x00000008) /* Error messages */
47 #define DBG_K_TRACE (DBG_K_TRACE_ENTRY|DBG_K_TRACE_EXIT)
48 #define DBG_K_STANDARD (DBG_K_INFO|DBG_K_ERROR|DBG_K_TRACE)
49 /* Redefine this flagword to set debug level */
50 #define DEBUG_LEVEL DBG_K_STANDARD
52 #define DEFINE_DBG_BUFFER char __dbg_str_buf[256];
54 #define DBG_PRINT( dbg_flags, args... ) \
56 if ( DEBUG_LEVEL & ( dbg_flags ) ) \
59 len = sprintf( __dbg_str_buf, "%s:%d: %s: ", \
60 __FILE__, __LINE__, __FUNCTION__ ); \
61 sprintf( __dbg_str_buf + len, args ); \
62 printk( KERN_NOTICE "%s\n", __dbg_str_buf ); \
66 #define DBG_ENTER_ROUTINE DBG_PRINT (DBG_K_TRACE_ENTRY, "%s", "[Entry]");
67 #define DBG_LEAVE_ROUTINE DBG_PRINT (DBG_K_TRACE_EXIT, "%s", "[Exit]");
69 #define DEFINE_DBG_BUFFER
70 #define DBG_ENTER_ROUTINE
71 #define DBG_LEAVE_ROUTINE
90 } __attribute__ ((packed));
92 /* offsets to the controller registers based on the above structure layout */
94 PCIECAPID = offsetof(struct ctrl_reg, cap_id),
95 NXTCAPPTR = offsetof(struct ctrl_reg, nxt_ptr),
96 CAPREG = offsetof(struct ctrl_reg, cap_reg),
97 DEVCAP = offsetof(struct ctrl_reg, dev_cap),
98 DEVCTRL = offsetof(struct ctrl_reg, dev_ctrl),
99 DEVSTATUS = offsetof(struct ctrl_reg, dev_status),
100 LNKCAP = offsetof(struct ctrl_reg, lnk_cap),
101 LNKCTRL = offsetof(struct ctrl_reg, lnk_ctrl),
102 LNKSTATUS = offsetof(struct ctrl_reg, lnk_status),
103 SLOTCAP = offsetof(struct ctrl_reg, slot_cap),
104 SLOTCTRL = offsetof(struct ctrl_reg, slot_ctrl),
105 SLOTSTATUS = offsetof(struct ctrl_reg, slot_status),
106 ROOTCTRL = offsetof(struct ctrl_reg, root_ctrl),
107 ROOTSTATUS = offsetof(struct ctrl_reg, root_status),
109 static int pcie_cap_base = 0; /* Base of the PCI Express capability item structure */
111 #define PCIE_CAP_ID(cb) ( cb + PCIECAPID )
112 #define NXT_CAP_PTR(cb) ( cb + NXTCAPPTR )
113 #define CAP_REG(cb) ( cb + CAPREG )
114 #define DEV_CAP(cb) ( cb + DEVCAP )
115 #define DEV_CTRL(cb) ( cb + DEVCTRL )
116 #define DEV_STATUS(cb) ( cb + DEVSTATUS )
117 #define LNK_CAP(cb) ( cb + LNKCAP )
118 #define LNK_CTRL(cb) ( cb + LNKCTRL )
119 #define LNK_STATUS(cb) ( cb + LNKSTATUS )
120 #define SLOT_CAP(cb) ( cb + SLOTCAP )
121 #define SLOT_CTRL(cb) ( cb + SLOTCTRL )
122 #define SLOT_STATUS(cb) ( cb + SLOTSTATUS )
123 #define ROOT_CTRL(cb) ( cb + ROOTCTRL )
124 #define ROOT_STATUS(cb) ( cb + ROOTSTATUS )
126 #define hp_register_read_word(pdev, reg , value) \
127 pci_read_config_word(pdev, reg, &value)
129 #define hp_register_read_dword(pdev, reg , value) \
130 pci_read_config_dword(pdev, reg, &value)
132 #define hp_register_write_word(pdev, reg , value) \
133 pci_write_config_word(pdev, reg, value)
135 #define hp_register_dwrite_word(pdev, reg , value) \
136 pci_write_config_dword(pdev, reg, value)
138 /* Field definitions in PCI Express Capabilities Register */
139 #define CAP_VER 0x000F
140 #define DEV_PORT_TYPE 0x00F0
141 #define SLOT_IMPL 0x0100
142 #define MSG_NUM 0x3E00
144 /* Device or Port Type */
145 #define NAT_ENDPT 0x00
146 #define LEG_ENDPT 0x01
147 #define ROOT_PORT 0x04
148 #define UP_STREAM 0x05
149 #define DN_STREAM 0x06
150 #define PCIE_PCI_BRDG 0x07
151 #define PCI_PCIE_BRDG 0x10
153 /* Field definitions in Device Capabilities Register */
154 #define DATTN_BUTTN_PRSN 0x1000
155 #define DATTN_LED_PRSN 0x2000
156 #define DPWR_LED_PRSN 0x4000
158 /* Field definitions in Link Capabilities Register */
159 #define MAX_LNK_SPEED 0x000F
160 #define MAX_LNK_WIDTH 0x03F0
162 /* Link Width Encoding */
171 /*Field definitions of Link Status Register */
172 #define LNK_SPEED 0x000F
173 #define NEG_LINK_WD 0x03F0
174 #define LNK_TRN_ERR 0x0400
175 #define LNK_TRN 0x0800
176 #define SLOT_CLK_CONF 0x1000
178 /* Field definitions in Slot Capabilities Register */
179 #define ATTN_BUTTN_PRSN 0x00000001
180 #define PWR_CTRL_PRSN 0x00000002
181 #define MRL_SENS_PRSN 0x00000004
182 #define ATTN_LED_PRSN 0x00000008
183 #define PWR_LED_PRSN 0x00000010
184 #define HP_SUPR_RM_SUP 0x00000020
185 #define HP_CAP 0x00000040
186 #define SLOT_PWR_VALUE 0x000003F8
187 #define SLOT_PWR_LIMIT 0x00000C00
188 #define PSN 0xFFF80000 /* PSN: Physical Slot Number */
190 /* Field definitions in Slot Control Register */
191 #define ATTN_BUTTN_ENABLE 0x0001
192 #define PWR_FAULT_DETECT_ENABLE 0x0002
193 #define MRL_DETECT_ENABLE 0x0004
194 #define PRSN_DETECT_ENABLE 0x0008
195 #define CMD_CMPL_INTR_ENABLE 0x0010
196 #define HP_INTR_ENABLE 0x0020
197 #define ATTN_LED_CTRL 0x00C0
198 #define PWR_LED_CTRL 0x0300
199 #define PWR_CTRL 0x0400
201 /* Attention indicator and Power indicator states */
203 #define LED_BLINK 0x10
206 /* Power Control Command */
208 #define POWER_OFF 0x0400
210 /* Field definitions in Slot Status Register */
211 #define ATTN_BUTTN_PRESSED 0x0001
212 #define PWR_FAULT_DETECTED 0x0002
213 #define MRL_SENS_CHANGED 0x0004
214 #define PRSN_DETECT_CHANGED 0x0008
215 #define CMD_COMPLETED 0x0010
216 #define MRL_STATE 0x0020
217 #define PRSN_STATE 0x0040
219 static spinlock_t hpc_event_lock;
221 DEFINE_DBG_BUFFER /* Debug string buffer for entire HPC defined here */
222 static struct php_ctlr_state_s *php_ctlr_list_head; /* HPC state linked list */
223 static int ctlr_seq_num = 0; /* Controller sequence # */
224 static spinlock_t list_lock;
226 static irqreturn_t pcie_isr(int IRQ, void *dev_id, struct pt_regs *regs);
228 static void start_int_poll_timer(struct php_ctlr_state_s *php_ctlr, int seconds);
230 /* This is the interrupt polling timeout function. */
231 static void int_poll_timeout(unsigned long lphp_ctlr)
233 struct php_ctlr_state_s *php_ctlr = (struct php_ctlr_state_s *)lphp_ctlr;
238 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
242 /* Poll for interrupt events. regs == NULL => polling */
243 pcie_isr( 0, (void *)php_ctlr, NULL );
245 init_timer(&php_ctlr->int_poll_timer);
247 if (!pciehp_poll_time)
248 pciehp_poll_time = 2; /* reset timer to poll in 2 secs if user doesn't specify at module installation*/
250 start_int_poll_timer(php_ctlr, pciehp_poll_time);
255 /* This function starts the interrupt polling timer. */
256 static void start_int_poll_timer(struct php_ctlr_state_s *php_ctlr, int seconds)
259 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
263 if ( ( seconds <= 0 ) || ( seconds > 60 ) )
264 seconds = 2; /* Clamp to sane value */
266 php_ctlr->int_poll_timer.function = &int_poll_timeout;
267 php_ctlr->int_poll_timer.data = (unsigned long)php_ctlr; /* Instance data */
268 php_ctlr->int_poll_timer.expires = jiffies + seconds * HZ;
269 add_timer(&php_ctlr->int_poll_timer);
274 static int pcie_write_cmd(struct slot *slot, u16 cmd)
276 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
283 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
287 retval = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS(slot->ctrl->cap_base), slot_status);
289 err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
293 if ((slot_status & CMD_COMPLETED) == CMD_COMPLETED ) {
294 /* After 1 sec and CMD_COMPLETED still not set, just proceed forward to issue
295 the next command according to spec. Just print out the error message */
296 dbg("%s : CMD_COMPLETED not clear after 1 sec.\n", __FUNCTION__);
299 retval = hp_register_write_word(php_ctlr->pci_dev, SLOT_CTRL(slot->ctrl->cap_base), cmd | CMD_CMPL_INTR_ENABLE);
301 err("%s : hp_register_write_word SLOT_CTRL failed\n", __FUNCTION__);
309 static int hpc_check_lnk_status(struct controller *ctrl)
311 struct php_ctlr_state_s *php_ctlr = ctrl->hpc_ctlr_handle;
318 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
322 retval = hp_register_read_word(php_ctlr->pci_dev, LNK_STATUS(ctrl->cap_base), lnk_status);
325 err("%s : hp_register_read_word LNK_STATUS failed\n", __FUNCTION__);
329 dbg("%s: lnk_status = %x\n", __FUNCTION__, lnk_status);
330 if ( (lnk_status & LNK_TRN) || (lnk_status & LNK_TRN_ERR) ||
331 !(lnk_status & NEG_LINK_WD)) {
332 err("%s : Link Training Error occurs \n", __FUNCTION__);
342 static int hpc_get_attention_status(struct slot *slot, u8 *status)
344 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
352 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
356 retval = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL(slot->ctrl->cap_base), slot_ctrl);
359 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
363 dbg("%s: SLOT_CTRL %x, value read %x\n", __FUNCTION__,SLOT_CTRL(slot->ctrl->cap_base), slot_ctrl);
365 atten_led_state = (slot_ctrl & ATTN_LED_CTRL) >> 6;
367 switch (atten_led_state) {
369 *status = 0xFF; /* Reserved */
372 *status = 1; /* On */
375 *status = 2; /* Blink */
378 *status = 0; /* Off */
389 static int hpc_get_power_status(struct slot * slot, u8 *status)
391 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
399 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
403 retval = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL(slot->ctrl->cap_base), slot_ctrl);
406 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
409 dbg("%s: SLOT_CTRL %x value read %x\n", __FUNCTION__, SLOT_CTRL(slot->ctrl->cap_base), slot_ctrl);
411 pwr_state = (slot_ctrl & PWR_CTRL) >> 10;
430 static int hpc_get_latch_status(struct slot *slot, u8 *status)
432 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
439 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
443 retval = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS(slot->ctrl->cap_base), slot_status);
446 err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
450 *status = (((slot_status & MRL_STATE) >> 5) == 0) ? 0 : 1;
456 static int hpc_get_adapter_status(struct slot *slot, u8 *status)
458 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
466 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
470 retval = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS(slot->ctrl->cap_base), slot_status);
473 err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
476 card_state = (u8)((slot_status & PRSN_STATE) >> 6);
477 *status = (card_state == 1) ? 1 : 0;
483 static int hpc_query_power_fault(struct slot * slot)
485 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
493 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
497 retval = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS(slot->ctrl->cap_base), slot_status);
500 err("%s : Cannot check for power fault\n", __FUNCTION__);
503 pwr_fault = (u8)((slot_status & PWR_FAULT_DETECTED) >> 1);
509 static int hpc_set_attention_status(struct slot *slot, u8 value)
511 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
519 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
523 if (slot->hp_slot >= php_ctlr->num_slots) {
524 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
527 rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL(slot->ctrl->cap_base), slot_ctrl);
530 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
535 case 0 : /* turn off */
536 slot_cmd = (slot_ctrl & ~ATTN_LED_CTRL) | 0x00C0;
538 case 1: /* turn on */
539 slot_cmd = (slot_ctrl & ~ATTN_LED_CTRL) | 0x0040;
541 case 2: /* turn blink */
542 slot_cmd = (slot_ctrl & ~ATTN_LED_CTRL) | 0x0080;
547 if (!pciehp_poll_mode)
548 slot_cmd = slot_cmd | HP_INTR_ENABLE;
550 pcie_write_cmd(slot, slot_cmd);
551 dbg("%s: SLOT_CTRL %x write cmd %x\n", __FUNCTION__, SLOT_CTRL(slot->ctrl->cap_base), slot_cmd);
558 static void hpc_set_green_led_on(struct slot *slot)
560 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
568 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
572 if (slot->hp_slot >= php_ctlr->num_slots) {
573 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
577 rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL(slot->ctrl->cap_base), slot_ctrl);
580 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
583 slot_cmd = (slot_ctrl & ~PWR_LED_CTRL) | 0x0100;
584 if (!pciehp_poll_mode)
585 slot_cmd = slot_cmd | HP_INTR_ENABLE;
587 pcie_write_cmd(slot, slot_cmd);
589 dbg("%s: SLOT_CTRL %x write cmd %x\n",__FUNCTION__, SLOT_CTRL(slot->ctrl->cap_base), slot_cmd);
594 static void hpc_set_green_led_off(struct slot *slot)
596 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
604 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
608 if (slot->hp_slot >= php_ctlr->num_slots) {
609 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
613 rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL(slot->ctrl->cap_base), slot_ctrl);
616 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
620 slot_cmd = (slot_ctrl & ~PWR_LED_CTRL) | 0x0300;
622 if (!pciehp_poll_mode)
623 slot_cmd = slot_cmd | HP_INTR_ENABLE;
624 pcie_write_cmd(slot, slot_cmd);
625 dbg("%s: SLOT_CTRL %x write cmd %x\n", __FUNCTION__, SLOT_CTRL(slot->ctrl->cap_base), slot_cmd);
631 static void hpc_set_green_led_blink(struct slot *slot)
633 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
641 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
645 if (slot->hp_slot >= php_ctlr->num_slots) {
646 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
650 rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL(slot->ctrl->cap_base), slot_ctrl);
653 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
657 slot_cmd = (slot_ctrl & ~PWR_LED_CTRL) | 0x0200;
659 if (!pciehp_poll_mode)
660 slot_cmd = slot_cmd | HP_INTR_ENABLE;
661 pcie_write_cmd(slot, slot_cmd);
663 dbg("%s: SLOT_CTRL %x write cmd %x\n",__FUNCTION__, SLOT_CTRL(slot->ctrl->cap_base), slot_cmd);
668 int pcie_get_ctlr_slot_config(struct controller *ctrl,
669 int *num_ctlr_slots, /* number of slots in this HPC; only 1 in PCIE */
670 int *first_device_num, /* PCI dev num of the first slot in this PCIE */
671 int *physical_slot_num, /* phy slot num of the first slot in this PCIE */
674 struct php_ctlr_state_s *php_ctlr = ctrl->hpc_ctlr_handle;
681 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
685 *first_device_num = 0;
688 rc = hp_register_read_dword(php_ctlr->pci_dev, SLOT_CAP(ctrl->cap_base), slot_cap);
691 err("%s : hp_register_read_dword SLOT_CAP failed\n", __FUNCTION__);
695 *physical_slot_num = slot_cap >> 19;
696 dbg("%s: PSN %d \n", __FUNCTION__, *physical_slot_num);
698 *ctrlcap = slot_cap & 0x0000007f;
704 static void hpc_release_ctlr(struct controller *ctrl)
706 struct php_ctlr_state_s *php_ctlr = ctrl->hpc_ctlr_handle;
707 struct php_ctlr_state_s *p, *p_prev;
712 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
716 if (pciehp_poll_mode) {
717 del_timer(&php_ctlr->int_poll_timer);
720 free_irq(php_ctlr->irq, ctrl);
723 pci_disable_msi(php_ctlr->pci_dev);
726 if (php_ctlr->pci_dev)
727 php_ctlr->pci_dev = NULL;
729 spin_lock(&list_lock);
730 p = php_ctlr_list_head;
735 p_prev->pnext = p->pnext;
737 php_ctlr_list_head = p->pnext;
744 spin_unlock(&list_lock);
752 static int hpc_power_on_slot(struct slot * slot)
754 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
756 u16 slot_ctrl, slot_status;
763 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
767 dbg("%s: slot->hp_slot %x\n", __FUNCTION__, slot->hp_slot);
768 if (slot->hp_slot >= php_ctlr->num_slots) {
769 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
773 /* Clear sticky power-fault bit from previous power failures */
774 hp_register_read_word(php_ctlr->pci_dev,
775 SLOT_STATUS(slot->ctrl->cap_base), slot_status);
776 slot_status &= PWR_FAULT_DETECTED;
778 hp_register_write_word(php_ctlr->pci_dev,
779 SLOT_STATUS(slot->ctrl->cap_base), slot_status);
781 retval = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL(slot->ctrl->cap_base), slot_ctrl);
784 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
788 slot_cmd = (slot_ctrl & ~PWR_CTRL) | POWER_ON;
790 /* Enable detection that we turned off at slot power-off time */
791 if (!pciehp_poll_mode)
792 slot_cmd = slot_cmd |
793 PWR_FAULT_DETECT_ENABLE |
798 retval = pcie_write_cmd(slot, slot_cmd);
801 err("%s: Write %x command failed!\n", __FUNCTION__, slot_cmd);
804 dbg("%s: SLOT_CTRL %x write cmd %x\n",__FUNCTION__, SLOT_CTRL(slot->ctrl->cap_base), slot_cmd);
811 static int hpc_power_off_slot(struct slot * slot)
813 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
822 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
826 dbg("%s: slot->hp_slot %x\n", __FUNCTION__, slot->hp_slot);
828 if (slot->hp_slot >= php_ctlr->num_slots) {
829 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
832 retval = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL(slot->ctrl->cap_base), slot_ctrl);
835 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
839 slot_cmd = (slot_ctrl & ~PWR_CTRL) | POWER_OFF;
842 * If we get MRL or presence detect interrupts now, the isr
843 * will notice the sticky power-fault bit too and issue power
844 * indicator change commands. This will lead to an endless loop
845 * of command completions, since the power-fault bit remains on
846 * till the slot is powered on again.
848 if (!pciehp_poll_mode)
849 slot_cmd = (slot_cmd &
850 ~PWR_FAULT_DETECT_ENABLE &
852 ~PRSN_DETECT_ENABLE) | HP_INTR_ENABLE;
854 retval = pcie_write_cmd(slot, slot_cmd);
857 err("%s: Write command failed!\n", __FUNCTION__);
860 dbg("%s: SLOT_CTRL %x write cmd %x\n",__FUNCTION__, SLOT_CTRL(slot->ctrl->cap_base), slot_cmd);
867 static irqreturn_t pcie_isr(int IRQ, void *dev_id, struct pt_regs *regs)
869 struct controller *ctrl = NULL;
870 struct php_ctlr_state_s *php_ctlr;
871 u8 schedule_flag = 0;
872 u16 slot_status, intr_detect, intr_loc;
874 int hp_slot = 0; /* only 1 slot per PCI Express port */
880 if (!pciehp_poll_mode) {
882 php_ctlr = ctrl->hpc_ctlr_handle;
885 ctrl = (struct controller *)php_ctlr->callback_instance_id;
889 dbg("%s: dev_id %p ctlr == NULL\n", __FUNCTION__, (void*) dev_id);
894 dbg("%s: php_ctlr == NULL\n", __FUNCTION__);
898 rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), slot_status);
900 err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
904 intr_detect = ( ATTN_BUTTN_PRESSED | PWR_FAULT_DETECTED | MRL_SENS_CHANGED |
905 PRSN_DETECT_CHANGED | CMD_COMPLETED );
907 intr_loc = slot_status & intr_detect;
909 /* Check to see if it was our interrupt */
913 dbg("%s: intr_loc %x\n", __FUNCTION__, intr_loc);
914 /* Mask Hot-plug Interrupt Enable */
915 if (!pciehp_poll_mode) {
916 rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL(ctrl->cap_base), temp_word);
918 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
922 dbg("%s: hp_register_read_word SLOT_CTRL with value %x\n", __FUNCTION__, temp_word);
923 temp_word = (temp_word & ~HP_INTR_ENABLE & ~CMD_CMPL_INTR_ENABLE) | 0x00;
925 rc = hp_register_write_word(php_ctlr->pci_dev, SLOT_CTRL(ctrl->cap_base), temp_word);
927 err("%s : hp_register_write_word SLOT_CTRL failed\n", __FUNCTION__);
931 rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), slot_status);
933 err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
936 dbg("%s: hp_register_read_word SLOT_STATUS with value %x\n", __FUNCTION__, slot_status);
938 /* Clear command complete interrupt caused by this write */
940 rc = hp_register_write_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), temp_word);
942 err("%s : hp_register_write_word SLOT_STATUS failed\n", __FUNCTION__);
947 if (intr_loc & CMD_COMPLETED) {
949 * Command Complete Interrupt Pending
951 wake_up_interruptible(&ctrl->queue);
954 if ((php_ctlr->switch_change_callback) && (intr_loc & MRL_SENS_CHANGED))
955 schedule_flag += php_ctlr->switch_change_callback(
956 hp_slot, php_ctlr->callback_instance_id);
957 if ((php_ctlr->attention_button_callback) && (intr_loc & ATTN_BUTTN_PRESSED))
958 schedule_flag += php_ctlr->attention_button_callback(
959 hp_slot, php_ctlr->callback_instance_id);
960 if ((php_ctlr->presence_change_callback) && (intr_loc & PRSN_DETECT_CHANGED))
961 schedule_flag += php_ctlr->presence_change_callback(
962 hp_slot , php_ctlr->callback_instance_id);
963 if ((php_ctlr->power_fault_callback) && (intr_loc & PWR_FAULT_DETECTED))
964 schedule_flag += php_ctlr->power_fault_callback(
965 hp_slot, php_ctlr->callback_instance_id);
967 /* Clear all events after serving them */
969 rc = hp_register_write_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), temp_word);
971 err("%s : hp_register_write_word SLOT_STATUS failed\n", __FUNCTION__);
974 /* Unmask Hot-plug Interrupt Enable */
975 if (!pciehp_poll_mode) {
976 rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL(ctrl->cap_base), temp_word);
978 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
982 dbg("%s: Unmask Hot-plug Interrupt Enable\n", __FUNCTION__);
983 temp_word = (temp_word & ~HP_INTR_ENABLE) | HP_INTR_ENABLE;
985 rc = hp_register_write_word(php_ctlr->pci_dev, SLOT_CTRL(ctrl->cap_base), temp_word);
987 err("%s : hp_register_write_word SLOT_CTRL failed\n", __FUNCTION__);
991 rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), slot_status);
993 err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
997 /* Clear command complete interrupt caused by this write */
999 rc = hp_register_write_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), temp_word);
1001 err("%s : hp_register_write_word SLOT_STATUS failed\n", __FUNCTION__);
1004 dbg("%s: hp_register_write_word SLOT_STATUS with value %x\n", __FUNCTION__, temp_word);
1010 static int hpc_get_max_lnk_speed (struct slot *slot, enum pci_bus_speed *value)
1012 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
1013 enum pcie_link_speed lnk_speed;
1020 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
1024 if (slot->hp_slot >= php_ctlr->num_slots) {
1025 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
1029 retval = hp_register_read_dword(php_ctlr->pci_dev, LNK_CAP(slot->ctrl->cap_base), lnk_cap);
1032 err("%s : hp_register_read_dword LNK_CAP failed\n", __FUNCTION__);
1036 switch (lnk_cap & 0x000F) {
1038 lnk_speed = PCIE_2PT5GB;
1041 lnk_speed = PCIE_LNK_SPEED_UNKNOWN;
1046 dbg("Max link speed = %d\n", lnk_speed);
1051 static int hpc_get_max_lnk_width (struct slot *slot, enum pcie_link_width *value)
1053 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
1054 enum pcie_link_width lnk_wdth;
1061 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
1065 if (slot->hp_slot >= php_ctlr->num_slots) {
1066 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
1070 retval = hp_register_read_dword(php_ctlr->pci_dev, LNK_CAP(slot->ctrl->cap_base), lnk_cap);
1073 err("%s : hp_register_read_dword LNK_CAP failed\n", __FUNCTION__);
1077 switch ((lnk_cap & 0x03F0) >> 4){
1079 lnk_wdth = PCIE_LNK_WIDTH_RESRV;
1082 lnk_wdth = PCIE_LNK_X1;
1085 lnk_wdth = PCIE_LNK_X2;
1088 lnk_wdth = PCIE_LNK_X4;
1091 lnk_wdth = PCIE_LNK_X8;
1094 lnk_wdth = PCIE_LNK_X12;
1097 lnk_wdth = PCIE_LNK_X16;
1100 lnk_wdth = PCIE_LNK_X32;
1103 lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
1108 dbg("Max link width = %d\n", lnk_wdth);
1113 static int hpc_get_cur_lnk_speed (struct slot *slot, enum pci_bus_speed *value)
1115 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
1116 enum pcie_link_speed lnk_speed = PCI_SPEED_UNKNOWN;
1123 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
1127 if (slot->hp_slot >= php_ctlr->num_slots) {
1128 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
1132 retval = hp_register_read_word(php_ctlr->pci_dev, LNK_STATUS(slot->ctrl->cap_base), lnk_status);
1135 err("%s : hp_register_read_word LNK_STATUS failed\n", __FUNCTION__);
1139 switch (lnk_status & 0x0F) {
1141 lnk_speed = PCIE_2PT5GB;
1144 lnk_speed = PCIE_LNK_SPEED_UNKNOWN;
1149 dbg("Current link speed = %d\n", lnk_speed);
1154 static int hpc_get_cur_lnk_width (struct slot *slot, enum pcie_link_width *value)
1156 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
1157 enum pcie_link_width lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
1164 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
1168 if (slot->hp_slot >= php_ctlr->num_slots) {
1169 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
1173 retval = hp_register_read_word(php_ctlr->pci_dev, LNK_STATUS(slot->ctrl->cap_base), lnk_status);
1176 err("%s : hp_register_read_word LNK_STATUS failed\n", __FUNCTION__);
1180 switch ((lnk_status & 0x03F0) >> 4){
1182 lnk_wdth = PCIE_LNK_WIDTH_RESRV;
1185 lnk_wdth = PCIE_LNK_X1;
1188 lnk_wdth = PCIE_LNK_X2;
1191 lnk_wdth = PCIE_LNK_X4;
1194 lnk_wdth = PCIE_LNK_X8;
1197 lnk_wdth = PCIE_LNK_X12;
1200 lnk_wdth = PCIE_LNK_X16;
1203 lnk_wdth = PCIE_LNK_X32;
1206 lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
1211 dbg("Current link width = %d\n", lnk_wdth);
1216 static struct hpc_ops pciehp_hpc_ops = {
1217 .power_on_slot = hpc_power_on_slot,
1218 .power_off_slot = hpc_power_off_slot,
1219 .set_attention_status = hpc_set_attention_status,
1220 .get_power_status = hpc_get_power_status,
1221 .get_attention_status = hpc_get_attention_status,
1222 .get_latch_status = hpc_get_latch_status,
1223 .get_adapter_status = hpc_get_adapter_status,
1225 .get_max_bus_speed = hpc_get_max_lnk_speed,
1226 .get_cur_bus_speed = hpc_get_cur_lnk_speed,
1227 .get_max_lnk_width = hpc_get_max_lnk_width,
1228 .get_cur_lnk_width = hpc_get_cur_lnk_width,
1230 .query_power_fault = hpc_query_power_fault,
1231 .green_led_on = hpc_set_green_led_on,
1232 .green_led_off = hpc_set_green_led_off,
1233 .green_led_blink = hpc_set_green_led_blink,
1235 .release_ctlr = hpc_release_ctlr,
1236 .check_lnk_status = hpc_check_lnk_status,
1239 int pcie_init(struct controller * ctrl, struct pcie_device *dev)
1241 struct php_ctlr_state_s *php_ctlr, *p;
1242 void *instance_id = ctrl;
1244 static int first = 1;
1247 u16 intr_enable = 0;
1249 int cap_base, saved_cap_base;
1250 u16 slot_status, slot_ctrl;
1251 struct pci_dev *pdev;
1255 spin_lock_init(&list_lock);
1256 php_ctlr = (struct php_ctlr_state_s *) kmalloc(sizeof(struct php_ctlr_state_s), GFP_KERNEL);
1258 if (!php_ctlr) { /* allocate controller state data */
1259 err("%s: HPC controller memory allocation error!\n", __FUNCTION__);
1263 memset(php_ctlr, 0, sizeof(struct php_ctlr_state_s));
1266 php_ctlr->pci_dev = pdev; /* save pci_dev in context */
1268 dbg("%s: hotplug controller vendor id 0x%x device id 0x%x\n",
1269 __FUNCTION__, pdev->vendor, pdev->device);
1271 saved_cap_base = pcie_cap_base;
1273 if ((cap_base = pci_find_capability(pdev, PCI_CAP_ID_EXP)) == 0) {
1274 dbg("%s: Can't find PCI_CAP_ID_EXP (0x10)\n", __FUNCTION__);
1275 goto abort_free_ctlr;
1278 ctrl->cap_base = cap_base;
1280 dbg("%s: pcie_cap_base %x\n", __FUNCTION__, pcie_cap_base);
1282 rc = hp_register_read_word(pdev, CAP_REG(ctrl->cap_base), cap_reg);
1284 err("%s : hp_register_read_word CAP_REG failed\n", __FUNCTION__);
1285 goto abort_free_ctlr;
1287 dbg("%s: CAP_REG offset %x cap_reg %x\n", __FUNCTION__, CAP_REG(ctrl->cap_base), cap_reg);
1289 if (((cap_reg & SLOT_IMPL) == 0) || (((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 connected to a slot\n", __FUNCTION__);
1292 goto abort_free_ctlr;
1295 rc = hp_register_read_dword(php_ctlr->pci_dev, SLOT_CAP(ctrl->cap_base), slot_cap);
1297 err("%s : hp_register_read_word CAP_REG failed\n", __FUNCTION__);
1298 goto abort_free_ctlr;
1300 dbg("%s: SLOT_CAP offset %x slot_cap %x\n", __FUNCTION__, SLOT_CAP(ctrl->cap_base), slot_cap);
1302 if (!(slot_cap & HP_CAP)) {
1303 dbg("%s : This slot is not hot-plug capable\n", __FUNCTION__);
1304 goto abort_free_ctlr;
1306 /* For debugging purpose */
1307 rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), slot_status);
1309 err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
1310 goto abort_free_ctlr;
1312 dbg("%s: SLOT_STATUS offset %x slot_status %x\n", __FUNCTION__, SLOT_STATUS(ctrl->cap_base), slot_status);
1314 rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL(ctrl->cap_base), slot_ctrl);
1316 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
1317 goto abort_free_ctlr;
1319 dbg("%s: SLOT_CTRL offset %x slot_ctrl %x\n", __FUNCTION__, SLOT_CTRL(ctrl->cap_base), slot_ctrl);
1322 spin_lock_init(&hpc_event_lock);
1326 for ( rc = 0; rc < DEVICE_COUNT_RESOURCE; rc++)
1327 if (pci_resource_len(pdev, rc) > 0)
1328 dbg("pci resource[%d] start=0x%lx(len=0x%lx)\n", rc,
1329 pci_resource_start(pdev, rc), pci_resource_len(pdev, rc));
1331 info("HPC vendor_id %x device_id %x ss_vid %x ss_did %x\n", pdev->vendor, pdev->device,
1332 pdev->subsystem_vendor, pdev->subsystem_device);
1334 if (pci_enable_device(pdev))
1335 goto abort_free_ctlr;
1337 init_MUTEX(&ctrl->crit_sect);
1338 /* setup wait queue */
1339 init_waitqueue_head(&ctrl->queue);
1342 php_ctlr->irq = dev->irq;
1344 /* Save interrupt callback info */
1345 php_ctlr->attention_button_callback = pciehp_handle_attention_button;
1346 php_ctlr->switch_change_callback = pciehp_handle_switch_change;
1347 php_ctlr->presence_change_callback = pciehp_handle_presence_change;
1348 php_ctlr->power_fault_callback = pciehp_handle_power_fault;
1349 php_ctlr->callback_instance_id = instance_id;
1351 /* return PCI Controller Info */
1352 php_ctlr->slot_device_offset = 0;
1353 php_ctlr->num_slots = 1;
1355 /* Mask Hot-plug Interrupt Enable */
1356 rc = hp_register_read_word(pdev, SLOT_CTRL(ctrl->cap_base), temp_word);
1358 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
1359 goto abort_free_ctlr;
1362 dbg("%s: SLOT_CTRL %x value read %x\n", __FUNCTION__, SLOT_CTRL(ctrl->cap_base), temp_word);
1363 temp_word = (temp_word & ~HP_INTR_ENABLE & ~CMD_CMPL_INTR_ENABLE) | 0x00;
1365 rc = hp_register_write_word(pdev, SLOT_CTRL(ctrl->cap_base), temp_word);
1367 err("%s : hp_register_write_word SLOT_CTRL failed\n", __FUNCTION__);
1368 goto abort_free_ctlr;
1371 rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), slot_status);
1373 err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
1374 goto abort_free_ctlr;
1377 temp_word = 0x1F; /* Clear all events */
1378 rc = hp_register_write_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), temp_word);
1380 err("%s : hp_register_write_word SLOT_STATUS failed\n", __FUNCTION__);
1381 goto abort_free_ctlr;
1384 if (pciehp_poll_mode) {/* Install interrupt polling code */
1385 /* Install and start the interrupt polling timer */
1386 init_timer(&php_ctlr->int_poll_timer);
1387 start_int_poll_timer( php_ctlr, 10 ); /* start with 10 second delay */
1389 /* Installs the interrupt handler */
1390 rc = request_irq(php_ctlr->irq, pcie_isr, SA_SHIRQ, MY_NAME, (void *) ctrl);
1391 dbg("%s: request_irq %d for hpc%d (returns %d)\n", __FUNCTION__, php_ctlr->irq, ctlr_seq_num, rc);
1393 err("Can't get irq %d for the hotplug controller\n", php_ctlr->irq);
1394 goto abort_free_ctlr;
1398 dbg("pciehp ctrl b:d:f:irq=0x%x:%x:%x:%x\n", pdev->bus->number,
1399 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn), dev->irq);
1401 rc = hp_register_read_word(pdev, SLOT_CTRL(ctrl->cap_base), temp_word);
1403 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
1404 goto abort_free_ctlr;
1407 intr_enable = intr_enable | PRSN_DETECT_ENABLE;
1409 if (ATTN_BUTTN(slot_cap))
1410 intr_enable = intr_enable | ATTN_BUTTN_ENABLE;
1412 if (POWER_CTRL(slot_cap))
1413 intr_enable = intr_enable | PWR_FAULT_DETECT_ENABLE;
1415 if (MRL_SENS(slot_cap))
1416 intr_enable = intr_enable | MRL_DETECT_ENABLE;
1418 temp_word = (temp_word & ~intr_enable) | intr_enable;
1420 if (pciehp_poll_mode) {
1421 temp_word = (temp_word & ~HP_INTR_ENABLE) | 0x0;
1423 temp_word = (temp_word & ~HP_INTR_ENABLE) | HP_INTR_ENABLE;
1426 /* Unmask Hot-plug Interrupt Enable for the interrupt notification mechanism case */
1427 rc = hp_register_write_word(pdev, SLOT_CTRL(ctrl->cap_base), temp_word);
1429 err("%s : hp_register_write_word SLOT_CTRL failed\n", __FUNCTION__);
1430 goto abort_free_ctlr;
1432 rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), slot_status);
1434 err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
1435 goto abort_free_ctlr;
1438 temp_word = 0x1F; /* Clear all events */
1439 rc = hp_register_write_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), temp_word);
1441 err("%s : hp_register_write_word SLOT_STATUS failed\n", __FUNCTION__);
1442 goto abort_free_ctlr;
1446 dbg("Bypassing BIOS check for pciehp use on %s\n",
1447 pci_name(ctrl->pci_dev));
1449 rc = pciehp_get_hp_hw_control_from_firmware(ctrl->pci_dev);
1451 goto abort_free_ctlr;
1454 /* Add this HPC instance into the HPC list */
1455 spin_lock(&list_lock);
1456 if (php_ctlr_list_head == 0) {
1457 php_ctlr_list_head = php_ctlr;
1458 p = php_ctlr_list_head;
1461 p = php_ctlr_list_head;
1466 p->pnext = php_ctlr;
1468 spin_unlock(&list_lock);
1471 ctrl->hpc_ctlr_handle = php_ctlr;
1472 ctrl->hpc_ops = &pciehp_hpc_ops;
1477 /* We end up here for the many possible ways to fail this API. */
1479 pcie_cap_base = saved_cap_base;