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/pci.h>
38 #define DBG_K_TRACE_ENTRY ((unsigned int)0x00000001) /* On function entry */
39 #define DBG_K_TRACE_EXIT ((unsigned int)0x00000002) /* On function exit */
40 #define DBG_K_INFO ((unsigned int)0x00000004) /* Info messages */
41 #define DBG_K_ERROR ((unsigned int)0x00000008) /* Error messages */
42 #define DBG_K_TRACE (DBG_K_TRACE_ENTRY|DBG_K_TRACE_EXIT)
43 #define DBG_K_STANDARD (DBG_K_INFO|DBG_K_ERROR|DBG_K_TRACE)
44 /* Redefine this flagword to set debug level */
45 #define DEBUG_LEVEL DBG_K_STANDARD
47 #define DEFINE_DBG_BUFFER char __dbg_str_buf[256];
49 #define DBG_PRINT( dbg_flags, args... ) \
51 if ( DEBUG_LEVEL & ( dbg_flags ) ) \
54 len = sprintf( __dbg_str_buf, "%s:%d: %s: ", \
55 __FILE__, __LINE__, __FUNCTION__ ); \
56 sprintf( __dbg_str_buf + len, args ); \
57 printk( KERN_NOTICE "%s\n", __dbg_str_buf ); \
61 #define DBG_ENTER_ROUTINE DBG_PRINT (DBG_K_TRACE_ENTRY, "%s", "[Entry]");
62 #define DBG_LEAVE_ROUTINE DBG_PRINT (DBG_K_TRACE_EXIT, "%s", "[Exit]");
64 #define DEFINE_DBG_BUFFER
65 #define DBG_ENTER_ROUTINE
66 #define DBG_LEAVE_ROUTINE
85 } __attribute__ ((packed));
87 /* offsets to the controller registers based on the above structure layout */
89 PCIECAPID = offsetof(struct ctrl_reg, cap_id),
90 NXTCAPPTR = offsetof(struct ctrl_reg, nxt_ptr),
91 CAPREG = offsetof(struct ctrl_reg, cap_reg),
92 DEVCAP = offsetof(struct ctrl_reg, dev_cap),
93 DEVCTRL = offsetof(struct ctrl_reg, dev_ctrl),
94 DEVSTATUS = offsetof(struct ctrl_reg, dev_status),
95 LNKCAP = offsetof(struct ctrl_reg, lnk_cap),
96 LNKCTRL = offsetof(struct ctrl_reg, lnk_ctrl),
97 LNKSTATUS = offsetof(struct ctrl_reg, lnk_status),
98 SLOTCAP = offsetof(struct ctrl_reg, slot_cap),
99 SLOTCTRL = offsetof(struct ctrl_reg, slot_ctrl),
100 SLOTSTATUS = offsetof(struct ctrl_reg, slot_status),
101 ROOTCTRL = offsetof(struct ctrl_reg, root_ctrl),
102 ROOTSTATUS = offsetof(struct ctrl_reg, root_status),
104 static int pcie_cap_base = 0; /* Base of the PCI Express capability item structure */
106 #define PCIE_CAP_ID(cb) ( cb + PCIECAPID )
107 #define NXT_CAP_PTR(cb) ( cb + NXTCAPPTR )
108 #define CAP_REG(cb) ( cb + CAPREG )
109 #define DEV_CAP(cb) ( cb + DEVCAP )
110 #define DEV_CTRL(cb) ( cb + DEVCTRL )
111 #define DEV_STATUS(cb) ( cb + DEVSTATUS )
112 #define LNK_CAP(cb) ( cb + LNKCAP )
113 #define LNK_CTRL(cb) ( cb + LNKCTRL )
114 #define LNK_STATUS(cb) ( cb + LNKSTATUS )
115 #define SLOT_CAP(cb) ( cb + SLOTCAP )
116 #define SLOT_CTRL(cb) ( cb + SLOTCTRL )
117 #define SLOT_STATUS(cb) ( cb + SLOTSTATUS )
118 #define ROOT_CTRL(cb) ( cb + ROOTCTRL )
119 #define ROOT_STATUS(cb) ( cb + ROOTSTATUS )
121 #define hp_register_read_word(pdev, reg , value) \
122 pci_read_config_word(pdev, reg, &value)
124 #define hp_register_read_dword(pdev, reg , value) \
125 pci_read_config_dword(pdev, reg, &value)
127 #define hp_register_write_word(pdev, reg , value) \
128 pci_write_config_word(pdev, reg, value)
130 #define hp_register_dwrite_word(pdev, reg , value) \
131 pci_write_config_dword(pdev, 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 struct php_ctlr_state_s *php_ctlr_list_head; /* HPC state linked list */
218 static int ctlr_seq_num = 0; /* Controller sequence # */
219 static spinlock_t list_lock;
221 static irqreturn_t pcie_isr(int IRQ, void *dev_id, struct pt_regs *regs);
223 static void start_int_poll_timer(struct php_ctlr_state_s *php_ctlr, int seconds);
225 /* This is the interrupt polling timeout function. */
226 static void int_poll_timeout(unsigned long lphp_ctlr)
228 struct php_ctlr_state_s *php_ctlr = (struct php_ctlr_state_s *)lphp_ctlr;
233 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
237 /* Poll for interrupt events. regs == NULL => polling */
238 pcie_isr( 0, (void *)php_ctlr, NULL );
240 init_timer(&php_ctlr->int_poll_timer);
242 if (!pciehp_poll_time)
243 pciehp_poll_time = 2; /* reset timer to poll in 2 secs if user doesn't specify at module installation*/
245 start_int_poll_timer(php_ctlr, pciehp_poll_time);
250 /* This function starts the interrupt polling timer. */
251 static void start_int_poll_timer(struct php_ctlr_state_s *php_ctlr, int seconds)
254 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
258 if ( ( seconds <= 0 ) || ( seconds > 60 ) )
259 seconds = 2; /* Clamp to sane value */
261 php_ctlr->int_poll_timer.function = &int_poll_timeout;
262 php_ctlr->int_poll_timer.data = (unsigned long)php_ctlr; /* Instance data */
263 php_ctlr->int_poll_timer.expires = jiffies + seconds * HZ;
264 add_timer(&php_ctlr->int_poll_timer);
269 static int pcie_write_cmd(struct slot *slot, u16 cmd)
271 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
278 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
282 retval = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS(slot->ctrl->cap_base), slot_status);
284 err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
288 if ((slot_status & CMD_COMPLETED) == CMD_COMPLETED ) {
289 /* After 1 sec and CMD_COMPLETED still not set, just proceed forward to issue
290 the next command according to spec. Just print out the error message */
291 dbg("%s : CMD_COMPLETED not clear after 1 sec.\n", __FUNCTION__);
294 retval = hp_register_write_word(php_ctlr->pci_dev, SLOT_CTRL(slot->ctrl->cap_base), cmd | CMD_CMPL_INTR_ENABLE);
296 err("%s : hp_register_write_word SLOT_CTRL failed\n", __FUNCTION__);
304 static int hpc_check_lnk_status(struct controller *ctrl)
306 struct php_ctlr_state_s *php_ctlr = ctrl->hpc_ctlr_handle;
313 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
317 retval = hp_register_read_word(php_ctlr->pci_dev, LNK_STATUS(ctrl->cap_base), lnk_status);
320 err("%s : hp_register_read_word LNK_STATUS failed\n", __FUNCTION__);
324 dbg("%s: lnk_status = %x\n", __FUNCTION__, lnk_status);
325 if ( (lnk_status & LNK_TRN) || (lnk_status & LNK_TRN_ERR) ||
326 !(lnk_status & NEG_LINK_WD)) {
327 err("%s : Link Training Error occurs \n", __FUNCTION__);
337 static int hpc_get_attention_status(struct slot *slot, u8 *status)
339 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
347 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
351 retval = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL(slot->ctrl->cap_base), slot_ctrl);
354 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
358 dbg("%s: SLOT_CTRL %x, value read %x\n", __FUNCTION__,SLOT_CTRL(slot->ctrl->cap_base), slot_ctrl);
360 atten_led_state = (slot_ctrl & ATTN_LED_CTRL) >> 6;
362 switch (atten_led_state) {
364 *status = 0xFF; /* Reserved */
367 *status = 1; /* On */
370 *status = 2; /* Blink */
373 *status = 0; /* Off */
384 static int hpc_get_power_status(struct slot * slot, u8 *status)
386 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
394 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
398 retval = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL(slot->ctrl->cap_base), slot_ctrl);
401 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
404 dbg("%s: SLOT_CTRL %x value read %x\n", __FUNCTION__, SLOT_CTRL(slot->ctrl->cap_base), slot_ctrl);
406 pwr_state = (slot_ctrl & PWR_CTRL) >> 10;
425 static int hpc_get_latch_status(struct slot *slot, u8 *status)
427 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
434 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
438 retval = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS(slot->ctrl->cap_base), slot_status);
441 err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
445 *status = (((slot_status & MRL_STATE) >> 5) == 0) ? 0 : 1;
451 static int hpc_get_adapter_status(struct slot *slot, u8 *status)
453 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
461 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
465 retval = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS(slot->ctrl->cap_base), slot_status);
468 err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
471 card_state = (u8)((slot_status & PRSN_STATE) >> 6);
472 *status = (card_state == 1) ? 1 : 0;
478 static int hpc_query_power_fault(struct slot * slot)
480 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
488 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
492 retval = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS(slot->ctrl->cap_base), slot_status);
495 err("%s : Cannot check for power fault\n", __FUNCTION__);
498 pwr_fault = (u8)((slot_status & PWR_FAULT_DETECTED) >> 1);
504 static int hpc_set_attention_status(struct slot *slot, u8 value)
506 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
514 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
518 if (slot->hp_slot >= php_ctlr->num_slots) {
519 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
522 rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL(slot->ctrl->cap_base), slot_ctrl);
525 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
530 case 0 : /* turn off */
531 slot_cmd = (slot_ctrl & ~ATTN_LED_CTRL) | 0x00C0;
533 case 1: /* turn on */
534 slot_cmd = (slot_ctrl & ~ATTN_LED_CTRL) | 0x0040;
536 case 2: /* turn blink */
537 slot_cmd = (slot_ctrl & ~ATTN_LED_CTRL) | 0x0080;
542 if (!pciehp_poll_mode)
543 slot_cmd = slot_cmd | HP_INTR_ENABLE;
545 pcie_write_cmd(slot, slot_cmd);
546 dbg("%s: SLOT_CTRL %x write cmd %x\n", __FUNCTION__, SLOT_CTRL(slot->ctrl->cap_base), slot_cmd);
553 static void hpc_set_green_led_on(struct slot *slot)
555 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
563 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
567 if (slot->hp_slot >= php_ctlr->num_slots) {
568 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
572 rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL(slot->ctrl->cap_base), slot_ctrl);
575 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
578 slot_cmd = (slot_ctrl & ~PWR_LED_CTRL) | 0x0100;
579 if (!pciehp_poll_mode)
580 slot_cmd = slot_cmd | HP_INTR_ENABLE;
582 pcie_write_cmd(slot, slot_cmd);
584 dbg("%s: SLOT_CTRL %x write cmd %x\n",__FUNCTION__, SLOT_CTRL(slot->ctrl->cap_base), slot_cmd);
589 static void hpc_set_green_led_off(struct slot *slot)
591 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
599 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
603 if (slot->hp_slot >= php_ctlr->num_slots) {
604 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
608 rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL(slot->ctrl->cap_base), slot_ctrl);
611 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
615 slot_cmd = (slot_ctrl & ~PWR_LED_CTRL) | 0x0300;
617 if (!pciehp_poll_mode)
618 slot_cmd = slot_cmd | HP_INTR_ENABLE;
619 pcie_write_cmd(slot, slot_cmd);
620 dbg("%s: SLOT_CTRL %x write cmd %x\n", __FUNCTION__, SLOT_CTRL(slot->ctrl->cap_base), slot_cmd);
626 static void hpc_set_green_led_blink(struct slot *slot)
628 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
636 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
640 if (slot->hp_slot >= php_ctlr->num_slots) {
641 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
645 rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL(slot->ctrl->cap_base), slot_ctrl);
648 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
652 slot_cmd = (slot_ctrl & ~PWR_LED_CTRL) | 0x0200;
654 if (!pciehp_poll_mode)
655 slot_cmd = slot_cmd | HP_INTR_ENABLE;
656 pcie_write_cmd(slot, slot_cmd);
658 dbg("%s: SLOT_CTRL %x write cmd %x\n",__FUNCTION__, SLOT_CTRL(slot->ctrl->cap_base), slot_cmd);
663 int pcie_get_ctlr_slot_config(struct controller *ctrl,
664 int *num_ctlr_slots, /* number of slots in this HPC; only 1 in PCIE */
665 int *first_device_num, /* PCI dev num of the first slot in this PCIE */
666 int *physical_slot_num, /* phy slot num of the first slot in this PCIE */
669 struct php_ctlr_state_s *php_ctlr = ctrl->hpc_ctlr_handle;
676 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
680 *first_device_num = 0;
683 rc = hp_register_read_dword(php_ctlr->pci_dev, SLOT_CAP(ctrl->cap_base), slot_cap);
686 err("%s : hp_register_read_dword SLOT_CAP failed\n", __FUNCTION__);
690 *physical_slot_num = slot_cap >> 19;
691 dbg("%s: PSN %d \n", __FUNCTION__, *physical_slot_num);
693 *ctrlcap = slot_cap & 0x0000007f;
699 static void hpc_release_ctlr(struct controller *ctrl)
701 struct php_ctlr_state_s *php_ctlr = ctrl->hpc_ctlr_handle;
702 struct php_ctlr_state_s *p, *p_prev;
707 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
711 if (pciehp_poll_mode) {
712 del_timer(&php_ctlr->int_poll_timer);
715 free_irq(php_ctlr->irq, ctrl);
718 pci_disable_msi(php_ctlr->pci_dev);
721 if (php_ctlr->pci_dev)
722 php_ctlr->pci_dev = NULL;
724 spin_lock(&list_lock);
725 p = php_ctlr_list_head;
730 p_prev->pnext = p->pnext;
732 php_ctlr_list_head = p->pnext;
739 spin_unlock(&list_lock);
747 static int hpc_power_on_slot(struct slot * slot)
749 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
758 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
762 dbg("%s: slot->hp_slot %x\n", __FUNCTION__, slot->hp_slot);
763 if (slot->hp_slot >= php_ctlr->num_slots) {
764 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
768 retval = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL(slot->ctrl->cap_base), slot_ctrl);
771 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
775 slot_cmd = (slot_ctrl & ~PWR_CTRL) | POWER_ON;
777 if (!pciehp_poll_mode)
778 slot_cmd = slot_cmd | HP_INTR_ENABLE;
780 retval = pcie_write_cmd(slot, slot_cmd);
783 err("%s: Write %x command failed!\n", __FUNCTION__, slot_cmd);
786 dbg("%s: SLOT_CTRL %x write cmd %x\n",__FUNCTION__, SLOT_CTRL(slot->ctrl->cap_base), slot_cmd);
793 static int hpc_power_off_slot(struct slot * slot)
795 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
804 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
808 dbg("%s: slot->hp_slot %x\n", __FUNCTION__, slot->hp_slot);
810 if (slot->hp_slot >= php_ctlr->num_slots) {
811 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
814 retval = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL(slot->ctrl->cap_base), slot_ctrl);
817 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
821 slot_cmd = (slot_ctrl & ~PWR_CTRL) | POWER_OFF;
823 if (!pciehp_poll_mode)
824 slot_cmd = slot_cmd | HP_INTR_ENABLE;
826 retval = pcie_write_cmd(slot, slot_cmd);
829 err("%s: Write command failed!\n", __FUNCTION__);
832 dbg("%s: SLOT_CTRL %x write cmd %x\n",__FUNCTION__, SLOT_CTRL(slot->ctrl->cap_base), slot_cmd);
839 static irqreturn_t pcie_isr(int IRQ, void *dev_id, struct pt_regs *regs)
841 struct controller *ctrl = NULL;
842 struct php_ctlr_state_s *php_ctlr;
843 u8 schedule_flag = 0;
844 u16 slot_status, intr_detect, intr_loc;
846 int hp_slot = 0; /* only 1 slot per PCI Express port */
852 if (!pciehp_poll_mode) {
854 php_ctlr = ctrl->hpc_ctlr_handle;
857 ctrl = (struct controller *)php_ctlr->callback_instance_id;
861 dbg("%s: dev_id %p ctlr == NULL\n", __FUNCTION__, (void*) dev_id);
866 dbg("%s: php_ctlr == NULL\n", __FUNCTION__);
870 rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), slot_status);
872 err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
876 intr_detect = ( ATTN_BUTTN_PRESSED | PWR_FAULT_DETECTED | MRL_SENS_CHANGED |
877 PRSN_DETECT_CHANGED | CMD_COMPLETED );
879 intr_loc = slot_status & intr_detect;
881 /* Check to see if it was our interrupt */
885 dbg("%s: intr_loc %x\n", __FUNCTION__, intr_loc);
886 /* Mask Hot-plug Interrupt Enable */
887 if (!pciehp_poll_mode) {
888 rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL(ctrl->cap_base), temp_word);
890 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
894 dbg("%s: hp_register_read_word SLOT_CTRL with value %x\n", __FUNCTION__, temp_word);
895 temp_word = (temp_word & ~HP_INTR_ENABLE & ~CMD_CMPL_INTR_ENABLE) | 0x00;
897 rc = hp_register_write_word(php_ctlr->pci_dev, SLOT_CTRL(ctrl->cap_base), temp_word);
899 err("%s : hp_register_write_word SLOT_CTRL failed\n", __FUNCTION__);
903 rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), slot_status);
905 err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
908 dbg("%s: hp_register_read_word SLOT_STATUS with value %x\n", __FUNCTION__, slot_status);
910 /* Clear command complete interrupt caused by this write */
912 rc = hp_register_write_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), temp_word);
914 err("%s : hp_register_write_word SLOT_STATUS failed\n", __FUNCTION__);
919 if (intr_loc & CMD_COMPLETED) {
921 * Command Complete Interrupt Pending
923 wake_up_interruptible(&ctrl->queue);
926 if ((php_ctlr->switch_change_callback) && (intr_loc & MRL_SENS_CHANGED))
927 schedule_flag += php_ctlr->switch_change_callback(
928 hp_slot, php_ctlr->callback_instance_id);
929 if ((php_ctlr->attention_button_callback) && (intr_loc & ATTN_BUTTN_PRESSED))
930 schedule_flag += php_ctlr->attention_button_callback(
931 hp_slot, php_ctlr->callback_instance_id);
932 if ((php_ctlr->presence_change_callback) && (intr_loc & PRSN_DETECT_CHANGED))
933 schedule_flag += php_ctlr->presence_change_callback(
934 hp_slot , php_ctlr->callback_instance_id);
935 if ((php_ctlr->power_fault_callback) && (intr_loc & PWR_FAULT_DETECTED))
936 schedule_flag += php_ctlr->power_fault_callback(
937 hp_slot, php_ctlr->callback_instance_id);
939 /* Clear all events after serving them */
941 rc = hp_register_write_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), temp_word);
943 err("%s : hp_register_write_word SLOT_STATUS failed\n", __FUNCTION__);
946 /* Unmask Hot-plug Interrupt Enable */
947 if (!pciehp_poll_mode) {
948 rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL(ctrl->cap_base), temp_word);
950 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
954 dbg("%s: Unmask Hot-plug Interrupt Enable\n", __FUNCTION__);
955 temp_word = (temp_word & ~HP_INTR_ENABLE) | HP_INTR_ENABLE;
957 rc = hp_register_write_word(php_ctlr->pci_dev, SLOT_CTRL(ctrl->cap_base), temp_word);
959 err("%s : hp_register_write_word SLOT_CTRL failed\n", __FUNCTION__);
963 rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), slot_status);
965 err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
969 /* Clear command complete interrupt caused by this write */
971 rc = hp_register_write_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), temp_word);
973 err("%s : hp_register_write_word SLOT_STATUS failed\n", __FUNCTION__);
976 dbg("%s: hp_register_write_word SLOT_STATUS with value %x\n", __FUNCTION__, temp_word);
982 static int hpc_get_max_lnk_speed (struct slot *slot, enum pci_bus_speed *value)
984 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
985 enum pcie_link_speed lnk_speed;
992 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
996 if (slot->hp_slot >= php_ctlr->num_slots) {
997 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
1001 retval = hp_register_read_dword(php_ctlr->pci_dev, LNK_CAP(slot->ctrl->cap_base), lnk_cap);
1004 err("%s : hp_register_read_dword LNK_CAP failed\n", __FUNCTION__);
1008 switch (lnk_cap & 0x000F) {
1010 lnk_speed = PCIE_2PT5GB;
1013 lnk_speed = PCIE_LNK_SPEED_UNKNOWN;
1018 dbg("Max link speed = %d\n", lnk_speed);
1023 static int hpc_get_max_lnk_width (struct slot *slot, enum pcie_link_width *value)
1025 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
1026 enum pcie_link_width lnk_wdth;
1033 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
1037 if (slot->hp_slot >= php_ctlr->num_slots) {
1038 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
1042 retval = hp_register_read_dword(php_ctlr->pci_dev, LNK_CAP(slot->ctrl->cap_base), lnk_cap);
1045 err("%s : hp_register_read_dword LNK_CAP failed\n", __FUNCTION__);
1049 switch ((lnk_cap & 0x03F0) >> 4){
1051 lnk_wdth = PCIE_LNK_WIDTH_RESRV;
1054 lnk_wdth = PCIE_LNK_X1;
1057 lnk_wdth = PCIE_LNK_X2;
1060 lnk_wdth = PCIE_LNK_X4;
1063 lnk_wdth = PCIE_LNK_X8;
1066 lnk_wdth = PCIE_LNK_X12;
1069 lnk_wdth = PCIE_LNK_X16;
1072 lnk_wdth = PCIE_LNK_X32;
1075 lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
1080 dbg("Max link width = %d\n", lnk_wdth);
1085 static int hpc_get_cur_lnk_speed (struct slot *slot, enum pci_bus_speed *value)
1087 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
1088 enum pcie_link_speed lnk_speed = PCI_SPEED_UNKNOWN;
1095 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
1099 if (slot->hp_slot >= php_ctlr->num_slots) {
1100 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
1104 retval = hp_register_read_word(php_ctlr->pci_dev, LNK_STATUS(slot->ctrl->cap_base), lnk_status);
1107 err("%s : hp_register_read_word LNK_STATUS failed\n", __FUNCTION__);
1111 switch (lnk_status & 0x0F) {
1113 lnk_speed = PCIE_2PT5GB;
1116 lnk_speed = PCIE_LNK_SPEED_UNKNOWN;
1121 dbg("Current link speed = %d\n", lnk_speed);
1126 static int hpc_get_cur_lnk_width (struct slot *slot, enum pcie_link_width *value)
1128 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
1129 enum pcie_link_width lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
1136 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
1140 if (slot->hp_slot >= php_ctlr->num_slots) {
1141 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
1145 retval = hp_register_read_word(php_ctlr->pci_dev, LNK_STATUS(slot->ctrl->cap_base), lnk_status);
1148 err("%s : hp_register_read_word LNK_STATUS failed\n", __FUNCTION__);
1152 switch ((lnk_status & 0x03F0) >> 4){
1154 lnk_wdth = PCIE_LNK_WIDTH_RESRV;
1157 lnk_wdth = PCIE_LNK_X1;
1160 lnk_wdth = PCIE_LNK_X2;
1163 lnk_wdth = PCIE_LNK_X4;
1166 lnk_wdth = PCIE_LNK_X8;
1169 lnk_wdth = PCIE_LNK_X12;
1172 lnk_wdth = PCIE_LNK_X16;
1175 lnk_wdth = PCIE_LNK_X32;
1178 lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
1183 dbg("Current link width = %d\n", lnk_wdth);
1188 static struct hpc_ops pciehp_hpc_ops = {
1189 .power_on_slot = hpc_power_on_slot,
1190 .power_off_slot = hpc_power_off_slot,
1191 .set_attention_status = hpc_set_attention_status,
1192 .get_power_status = hpc_get_power_status,
1193 .get_attention_status = hpc_get_attention_status,
1194 .get_latch_status = hpc_get_latch_status,
1195 .get_adapter_status = hpc_get_adapter_status,
1197 .get_max_bus_speed = hpc_get_max_lnk_speed,
1198 .get_cur_bus_speed = hpc_get_cur_lnk_speed,
1199 .get_max_lnk_width = hpc_get_max_lnk_width,
1200 .get_cur_lnk_width = hpc_get_cur_lnk_width,
1202 .query_power_fault = hpc_query_power_fault,
1203 .green_led_on = hpc_set_green_led_on,
1204 .green_led_off = hpc_set_green_led_off,
1205 .green_led_blink = hpc_set_green_led_blink,
1207 .release_ctlr = hpc_release_ctlr,
1208 .check_lnk_status = hpc_check_lnk_status,
1211 int pcie_init(struct controller * ctrl, struct pcie_device *dev)
1213 struct php_ctlr_state_s *php_ctlr, *p;
1214 void *instance_id = ctrl;
1216 static int first = 1;
1219 u16 intr_enable = 0;
1221 int cap_base, saved_cap_base;
1222 u16 slot_status, slot_ctrl;
1223 struct pci_dev *pdev;
1227 spin_lock_init(&list_lock);
1228 php_ctlr = (struct php_ctlr_state_s *) kmalloc(sizeof(struct php_ctlr_state_s), GFP_KERNEL);
1230 if (!php_ctlr) { /* allocate controller state data */
1231 err("%s: HPC controller memory allocation error!\n", __FUNCTION__);
1235 memset(php_ctlr, 0, sizeof(struct php_ctlr_state_s));
1238 php_ctlr->pci_dev = pdev; /* save pci_dev in context */
1240 dbg("%s: hotplug controller vendor id 0x%x device id 0x%x\n",
1241 __FUNCTION__, pdev->vendor, pdev->device);
1243 saved_cap_base = pcie_cap_base;
1245 if ((cap_base = pci_find_capability(pdev, PCI_CAP_ID_EXP)) == 0) {
1246 dbg("%s: Can't find PCI_CAP_ID_EXP (0x10)\n", __FUNCTION__);
1247 goto abort_free_ctlr;
1250 ctrl->cap_base = cap_base;
1252 dbg("%s: pcie_cap_base %x\n", __FUNCTION__, pcie_cap_base);
1254 rc = hp_register_read_word(pdev, CAP_REG(ctrl->cap_base), cap_reg);
1256 err("%s : hp_register_read_word CAP_REG failed\n", __FUNCTION__);
1257 goto abort_free_ctlr;
1259 dbg("%s: CAP_REG offset %x cap_reg %x\n", __FUNCTION__, CAP_REG(ctrl->cap_base), cap_reg);
1261 if (((cap_reg & SLOT_IMPL) == 0) || (((cap_reg & DEV_PORT_TYPE) != 0x0040)
1262 && ((cap_reg & DEV_PORT_TYPE) != 0x0060))) {
1263 dbg("%s : This is not a root port or the port is not connected to a slot\n", __FUNCTION__);
1264 goto abort_free_ctlr;
1267 rc = hp_register_read_dword(php_ctlr->pci_dev, SLOT_CAP(ctrl->cap_base), slot_cap);
1269 err("%s : hp_register_read_word CAP_REG failed\n", __FUNCTION__);
1270 goto abort_free_ctlr;
1272 dbg("%s: SLOT_CAP offset %x slot_cap %x\n", __FUNCTION__, SLOT_CAP(ctrl->cap_base), slot_cap);
1274 if (!(slot_cap & HP_CAP)) {
1275 dbg("%s : This slot is not hot-plug capable\n", __FUNCTION__);
1276 goto abort_free_ctlr;
1278 /* For debugging purpose */
1279 rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), slot_status);
1281 err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
1282 goto abort_free_ctlr;
1284 dbg("%s: SLOT_STATUS offset %x slot_status %x\n", __FUNCTION__, SLOT_STATUS(ctrl->cap_base), slot_status);
1286 rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL(ctrl->cap_base), slot_ctrl);
1288 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
1289 goto abort_free_ctlr;
1291 dbg("%s: SLOT_CTRL offset %x slot_ctrl %x\n", __FUNCTION__, SLOT_CTRL(ctrl->cap_base), slot_ctrl);
1294 spin_lock_init(&hpc_event_lock);
1298 for ( rc = 0; rc < DEVICE_COUNT_RESOURCE; rc++)
1299 if (pci_resource_len(pdev, rc) > 0)
1300 dbg("pci resource[%d] start=0x%lx(len=0x%lx)\n", rc,
1301 pci_resource_start(pdev, rc), pci_resource_len(pdev, rc));
1303 info("HPC vendor_id %x device_id %x ss_vid %x ss_did %x\n", pdev->vendor, pdev->device,
1304 pdev->subsystem_vendor, pdev->subsystem_device);
1306 if (pci_enable_device(pdev))
1307 goto abort_free_ctlr;
1309 init_MUTEX(&ctrl->crit_sect);
1310 /* setup wait queue */
1311 init_waitqueue_head(&ctrl->queue);
1314 php_ctlr->irq = dev->irq;
1316 /* Save interrupt callback info */
1317 php_ctlr->attention_button_callback = pciehp_handle_attention_button;
1318 php_ctlr->switch_change_callback = pciehp_handle_switch_change;
1319 php_ctlr->presence_change_callback = pciehp_handle_presence_change;
1320 php_ctlr->power_fault_callback = pciehp_handle_power_fault;
1321 php_ctlr->callback_instance_id = instance_id;
1323 /* return PCI Controller Info */
1324 php_ctlr->slot_device_offset = 0;
1325 php_ctlr->num_slots = 1;
1327 /* Mask Hot-plug Interrupt Enable */
1328 rc = hp_register_read_word(pdev, SLOT_CTRL(ctrl->cap_base), temp_word);
1330 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
1331 goto abort_free_ctlr;
1334 dbg("%s: SLOT_CTRL %x value read %x\n", __FUNCTION__, SLOT_CTRL(ctrl->cap_base), temp_word);
1335 temp_word = (temp_word & ~HP_INTR_ENABLE & ~CMD_CMPL_INTR_ENABLE) | 0x00;
1337 rc = hp_register_write_word(pdev, SLOT_CTRL(ctrl->cap_base), temp_word);
1339 err("%s : hp_register_write_word SLOT_CTRL failed\n", __FUNCTION__);
1340 goto abort_free_ctlr;
1343 rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), slot_status);
1345 err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
1346 goto abort_free_ctlr;
1349 temp_word = 0x1F; /* Clear all events */
1350 rc = hp_register_write_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), temp_word);
1352 err("%s : hp_register_write_word SLOT_STATUS failed\n", __FUNCTION__);
1353 goto abort_free_ctlr;
1356 if (pciehp_poll_mode) {/* Install interrupt polling code */
1357 /* Install and start the interrupt polling timer */
1358 init_timer(&php_ctlr->int_poll_timer);
1359 start_int_poll_timer( php_ctlr, 10 ); /* start with 10 second delay */
1361 /* Installs the interrupt handler */
1362 rc = request_irq(php_ctlr->irq, pcie_isr, SA_SHIRQ, MY_NAME, (void *) ctrl);
1363 dbg("%s: request_irq %d for hpc%d (returns %d)\n", __FUNCTION__, php_ctlr->irq, ctlr_seq_num, rc);
1365 err("Can't get irq %d for the hotplug controller\n", php_ctlr->irq);
1366 goto abort_free_ctlr;
1370 dbg("pciehp ctrl b:d:f:irq=0x%x:%x:%x:%x\n", pdev->bus->number,
1371 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn), dev->irq);
1373 rc = hp_register_read_word(pdev, SLOT_CTRL(ctrl->cap_base), temp_word);
1375 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
1376 goto abort_free_ctlr;
1379 intr_enable = intr_enable | PRSN_DETECT_ENABLE;
1381 if (ATTN_BUTTN(slot_cap))
1382 intr_enable = intr_enable | ATTN_BUTTN_ENABLE;
1384 if (POWER_CTRL(slot_cap))
1385 intr_enable = intr_enable | PWR_FAULT_DETECT_ENABLE;
1387 if (MRL_SENS(slot_cap))
1388 intr_enable = intr_enable | MRL_DETECT_ENABLE;
1390 temp_word = (temp_word & ~intr_enable) | intr_enable;
1392 if (pciehp_poll_mode) {
1393 temp_word = (temp_word & ~HP_INTR_ENABLE) | 0x0;
1395 temp_word = (temp_word & ~HP_INTR_ENABLE) | HP_INTR_ENABLE;
1398 /* Unmask Hot-plug Interrupt Enable for the interrupt notification mechanism case */
1399 rc = hp_register_write_word(pdev, SLOT_CTRL(ctrl->cap_base), temp_word);
1401 err("%s : hp_register_write_word SLOT_CTRL failed\n", __FUNCTION__);
1402 goto abort_free_ctlr;
1404 rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), slot_status);
1406 err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
1407 goto abort_free_ctlr;
1410 temp_word = 0x1F; /* Clear all events */
1411 rc = hp_register_write_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), temp_word);
1413 err("%s : hp_register_write_word SLOT_STATUS failed\n", __FUNCTION__);
1414 goto abort_free_ctlr;
1418 dbg("Bypassing BIOS check for pciehp use on %s\n",
1419 pci_name(ctrl->pci_dev));
1421 rc = pciehp_get_hp_hw_control_from_firmware(ctrl->pci_dev);
1423 goto abort_free_ctlr;
1426 /* Add this HPC instance into the HPC list */
1427 spin_lock(&list_lock);
1428 if (php_ctlr_list_head == 0) {
1429 php_ctlr_list_head = php_ctlr;
1430 p = php_ctlr_list_head;
1433 p = php_ctlr_list_head;
1438 p->pnext = php_ctlr;
1440 spin_unlock(&list_lock);
1443 ctrl->hpc_ctlr_handle = php_ctlr;
1444 ctrl->hpc_ops = &pciehp_hpc_ops;
1449 /* We end up here for the many possible ways to fail this API. */
1451 pcie_cap_base = saved_cap_base;