blob: 9fbd9b9f98248948c81ffd0a6835074fb1f9c87c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * PCI Express PCI Hot Plug Driver
3 *
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
8 *
9 * All rights reserved.
10 *
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.
15 *
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
20 * details.
21 *
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.
25 *
Kristen Accardi8cf4c192005-08-16 15:16:10 -070026 * Send feedback to <greg@kroah.com>,<kristen.c.accardi@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 *
28 */
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/kernel.h>
31#include <linux/module.h>
32#include <linux/types.h>
Tim Schmielaude259682006-01-08 01:02:05 -080033#include <linux/signal.h>
34#include <linux/jiffies.h>
35#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/pci.h>
Andrew Morton5d1b8c92005-11-13 16:06:39 -080037#include <linux/interrupt.h>
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "../pci.h"
40#include "pciehp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#ifdef DEBUG
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
50
51#define DEFINE_DBG_BUFFER char __dbg_str_buf[256];
52
53#define DBG_PRINT( dbg_flags, args... ) \
54 do { \
55 if ( DEBUG_LEVEL & ( dbg_flags ) ) \
56 { \
57 int len; \
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 ); \
62 } \
63 } while (0)
64
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]");
67#else
68#define DEFINE_DBG_BUFFER
69#define DBG_ENTER_ROUTINE
70#define DBG_LEAVE_ROUTINE
71#endif /* DEBUG */
72
73struct ctrl_reg {
74 u8 cap_id;
75 u8 nxt_ptr;
76 u16 cap_reg;
77 u32 dev_cap;
78 u16 dev_ctrl;
79 u16 dev_status;
80 u32 lnk_cap;
81 u16 lnk_ctrl;
82 u16 lnk_status;
83 u32 slot_cap;
84 u16 slot_ctrl;
85 u16 slot_status;
86 u16 root_ctrl;
87 u16 rsvp;
88 u32 root_status;
89} __attribute__ ((packed));
90
91/* offsets to the controller registers based on the above structure layout */
92enum ctrl_offsets {
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),
107};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800109static inline int pciehp_readw(struct controller *ctrl, int reg, u16 *value)
110{
111 struct pci_dev *dev = ctrl->pci_dev;
112 return pci_read_config_word(dev, ctrl->cap_base + reg, value);
113}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800115static inline int pciehp_readl(struct controller *ctrl, int reg, u32 *value)
116{
117 struct pci_dev *dev = ctrl->pci_dev;
118 return pci_read_config_dword(dev, ctrl->cap_base + reg, value);
119}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800121static inline int pciehp_writew(struct controller *ctrl, int reg, u16 value)
122{
123 struct pci_dev *dev = ctrl->pci_dev;
124 return pci_write_config_word(dev, ctrl->cap_base + reg, value);
125}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800127static inline int pciehp_writel(struct controller *ctrl, int reg, u32 value)
128{
129 struct pci_dev *dev = ctrl->pci_dev;
130 return pci_write_config_dword(dev, ctrl->cap_base + reg, value);
131}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
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
138
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
147
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
152
153/* Field definitions in Link Capabilities Register */
154#define MAX_LNK_SPEED 0x000F
155#define MAX_LNK_WIDTH 0x03F0
156
157/* Link Width Encoding */
158#define LNK_X1 0x01
159#define LNK_X2 0x02
160#define LNK_X4 0x04
161#define LNK_X8 0x08
162#define LNK_X12 0x0C
163#define LNK_X16 0x10
164#define LNK_X32 0x20
165
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
172
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 */
184
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
195
196/* Attention indicator and Power indicator states */
197#define LED_ON 0x01
198#define LED_BLINK 0x10
199#define LED_OFF 0x11
200
201/* Power Control Command */
202#define POWER_ON 0
203#define POWER_OFF 0x0400
204
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
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214static spinlock_t hpc_event_lock;
215
216DEFINE_DBG_BUFFER /* Debug string buffer for entire HPC defined here */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217static int ctlr_seq_num = 0; /* Controller sequence # */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800219static irqreturn_t pcie_isr(int irq, void *dev_id);
220static void start_int_poll_timer(struct controller *ctrl, int sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222/* This is the interrupt polling timeout function. */
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800223static void int_poll_timeout(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800225 struct controller *ctrl = (struct controller *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 DBG_ENTER_ROUTINE
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 /* Poll for interrupt events. regs == NULL => polling */
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800230 pcie_isr(0, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800232 init_timer(&ctrl->poll_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (!pciehp_poll_time)
234 pciehp_poll_time = 2; /* reset timer to poll in 2 secs if user doesn't specify at module installation*/
235
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800236 start_int_poll_timer(ctrl, pciehp_poll_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
239/* This function starts the interrupt polling timer. */
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800240static void start_int_poll_timer(struct controller *ctrl, int sec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800242 /* Clamp to sane value */
243 if ((sec <= 0) || (sec > 60))
244 sec = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800246 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800252static inline int pcie_wait_cmd(struct controller *ctrl)
253{
254 DECLARE_WAITQUEUE(wait, current);
255
256 add_wait_queue(&ctrl->queue, &wait);
257 if (!pciehp_poll_mode)
258 /* Sleep for up to 1 second */
259 msleep_interruptible(1000);
260 else
261 msleep_interruptible(2500);
262
263 remove_wait_queue(&ctrl->queue, &wait);
264 if (signal_pending(current))
265 return -EINTR;
266
267 return 0;
268}
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270static int pcie_write_cmd(struct slot *slot, u16 cmd)
271{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800272 struct controller *ctrl = slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 int retval = 0;
274 u16 slot_status;
275
276 DBG_ENTER_ROUTINE
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800277
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800278 mutex_lock(&ctrl->ctrl_lock);
279
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800280 retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (retval) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800282 err("%s: Cannot read SLOTSTATUS register\n", __FUNCTION__);
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800283 goto out;
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800284 }
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 if ((slot_status & CMD_COMPLETED) == CMD_COMPLETED ) {
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800287 /* After 1 sec and CMD_COMPLETED still not set, just
288 proceed forward to issue the next command according
289 to spec. Just print out the error message */
290 dbg("%s: CMD_COMPLETED not clear after 1 sec.\n",
291 __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
293
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800294 retval = pciehp_writew(ctrl, SLOTCTRL, (cmd | CMD_CMPL_INTR_ENABLE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (retval) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800296 err("%s: Cannot write to SLOTCTRL register\n", __FUNCTION__);
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800297 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800300 /*
301 * Wait for command completion.
302 */
303 retval = pcie_wait_cmd(ctrl);
304 out:
305 mutex_unlock(&ctrl->ctrl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 DBG_LEAVE_ROUTINE
307 return retval;
308}
309
310static int hpc_check_lnk_status(struct controller *ctrl)
311{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 u16 lnk_status;
313 int retval = 0;
314
315 DBG_ENTER_ROUTINE
316
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800317 retval = pciehp_readw(ctrl, LNKSTATUS, &lnk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (retval) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800319 err("%s: Cannot read LNKSTATUS register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return retval;
321 }
322
323 dbg("%s: lnk_status = %x\n", __FUNCTION__, lnk_status);
324 if ( (lnk_status & LNK_TRN) || (lnk_status & LNK_TRN_ERR) ||
325 !(lnk_status & NEG_LINK_WD)) {
326 err("%s : Link Training Error occurs \n", __FUNCTION__);
327 retval = -1;
328 return retval;
329 }
330
331 DBG_LEAVE_ROUTINE
332 return retval;
333}
334
335
336static int hpc_get_attention_status(struct slot *slot, u8 *status)
337{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800338 struct controller *ctrl = slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 u16 slot_ctrl;
340 u8 atten_led_state;
341 int retval = 0;
342
343 DBG_ENTER_ROUTINE
344
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800345 retval = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (retval) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800347 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return retval;
349 }
350
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800351 dbg("%s: SLOTCTRL %x, value read %x\n",
352 __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 atten_led_state = (slot_ctrl & ATTN_LED_CTRL) >> 6;
355
356 switch (atten_led_state) {
357 case 0:
358 *status = 0xFF; /* Reserved */
359 break;
360 case 1:
361 *status = 1; /* On */
362 break;
363 case 2:
364 *status = 2; /* Blink */
365 break;
366 case 3:
367 *status = 0; /* Off */
368 break;
369 default:
370 *status = 0xFF;
371 break;
372 }
373
374 DBG_LEAVE_ROUTINE
375 return 0;
376}
377
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800378static int hpc_get_power_status(struct slot *slot, u8 *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800380 struct controller *ctrl = slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 u16 slot_ctrl;
382 u8 pwr_state;
383 int retval = 0;
384
385 DBG_ENTER_ROUTINE
386
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800387 retval = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 if (retval) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800389 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return retval;
391 }
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800392 dbg("%s: SLOTCTRL %x value read %x\n",
393 __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 pwr_state = (slot_ctrl & PWR_CTRL) >> 10;
396
397 switch (pwr_state) {
398 case 0:
399 *status = 1;
400 break;
401 case 1:
402 *status = 0;
403 break;
404 default:
405 *status = 0xFF;
406 break;
407 }
408
409 DBG_LEAVE_ROUTINE
410 return retval;
411}
412
413
414static int hpc_get_latch_status(struct slot *slot, u8 *status)
415{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800416 struct controller *ctrl = slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 u16 slot_status;
418 int retval = 0;
419
420 DBG_ENTER_ROUTINE
421
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800422 retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (retval) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800424 err("%s: Cannot read SLOTSTATUS register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return retval;
426 }
427
428 *status = (((slot_status & MRL_STATE) >> 5) == 0) ? 0 : 1;
429
430 DBG_LEAVE_ROUTINE
431 return 0;
432}
433
434static int hpc_get_adapter_status(struct slot *slot, u8 *status)
435{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800436 struct controller *ctrl = slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 u16 slot_status;
438 u8 card_state;
439 int retval = 0;
440
441 DBG_ENTER_ROUTINE
442
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800443 retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 if (retval) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800445 err("%s: Cannot read SLOTSTATUS register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return retval;
447 }
448 card_state = (u8)((slot_status & PRSN_STATE) >> 6);
449 *status = (card_state == 1) ? 1 : 0;
450
451 DBG_LEAVE_ROUTINE
452 return 0;
453}
454
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800455static int hpc_query_power_fault(struct slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800457 struct controller *ctrl = slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 u16 slot_status;
459 u8 pwr_fault;
460 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 DBG_ENTER_ROUTINE
463
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800464 retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (retval) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800466 err("%s: Cannot check for power fault\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return retval;
468 }
469 pwr_fault = (u8)((slot_status & PWR_FAULT_DETECTED) >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 DBG_LEAVE_ROUTINE
rajesh.shah@intel.com8239def2005-10-31 16:20:13 -0800472 return pwr_fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
475static int hpc_set_attention_status(struct slot *slot, u8 value)
476{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800477 struct controller *ctrl = slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 u16 slot_cmd = 0;
479 u16 slot_ctrl;
480 int rc = 0;
481
rajesh.shah@intel.com1a9ed1b2005-10-31 16:20:10 -0800482 DBG_ENTER_ROUTINE
483
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800484 rc = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800486 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return rc;
488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 switch (value) {
491 case 0 : /* turn off */
492 slot_cmd = (slot_ctrl & ~ATTN_LED_CTRL) | 0x00C0;
493 break;
494 case 1: /* turn on */
495 slot_cmd = (slot_ctrl & ~ATTN_LED_CTRL) | 0x0040;
496 break;
497 case 2: /* turn blink */
498 slot_cmd = (slot_ctrl & ~ATTN_LED_CTRL) | 0x0080;
499 break;
500 default:
501 return -1;
502 }
503 if (!pciehp_poll_mode)
504 slot_cmd = slot_cmd | HP_INTR_ENABLE;
505
506 pcie_write_cmd(slot, slot_cmd);
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800507 dbg("%s: SLOTCTRL %x write cmd %x\n",
508 __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
rajesh.shah@intel.com1a9ed1b2005-10-31 16:20:10 -0800510 DBG_LEAVE_ROUTINE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return rc;
512}
513
514
515static void hpc_set_green_led_on(struct slot *slot)
516{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800517 struct controller *ctrl = slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 u16 slot_cmd;
519 u16 slot_ctrl;
520 int rc = 0;
521
rajesh.shah@intel.com1a9ed1b2005-10-31 16:20:10 -0800522 DBG_ENTER_ROUTINE
523
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800524 rc = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800526 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 return;
528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 slot_cmd = (slot_ctrl & ~PWR_LED_CTRL) | 0x0100;
530 if (!pciehp_poll_mode)
531 slot_cmd = slot_cmd | HP_INTR_ENABLE;
532
533 pcie_write_cmd(slot, slot_cmd);
534
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800535 dbg("%s: SLOTCTRL %x write cmd %x\n",
536 __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_cmd);
rajesh.shah@intel.com1a9ed1b2005-10-31 16:20:10 -0800537 DBG_LEAVE_ROUTINE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return;
539}
540
541static void hpc_set_green_led_off(struct slot *slot)
542{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800543 struct controller *ctrl = slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 u16 slot_cmd;
545 u16 slot_ctrl;
546 int rc = 0;
547
rajesh.shah@intel.com1a9ed1b2005-10-31 16:20:10 -0800548 DBG_ENTER_ROUTINE
549
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800550 rc = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800552 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return;
554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 slot_cmd = (slot_ctrl & ~PWR_LED_CTRL) | 0x0300;
557
558 if (!pciehp_poll_mode)
559 slot_cmd = slot_cmd | HP_INTR_ENABLE;
560 pcie_write_cmd(slot, slot_cmd);
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800561 dbg("%s: SLOTCTRL %x write cmd %x\n",
562 __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
rajesh.shah@intel.com1a9ed1b2005-10-31 16:20:10 -0800564 DBG_LEAVE_ROUTINE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return;
566}
567
568static void hpc_set_green_led_blink(struct slot *slot)
569{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800570 struct controller *ctrl = slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 u16 slot_cmd;
572 u16 slot_ctrl;
573 int rc = 0;
574
rajesh.shah@intel.com1a9ed1b2005-10-31 16:20:10 -0800575 DBG_ENTER_ROUTINE
576
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800577 rc = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800579 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return;
581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 slot_cmd = (slot_ctrl & ~PWR_LED_CTRL) | 0x0200;
584
585 if (!pciehp_poll_mode)
586 slot_cmd = slot_cmd | HP_INTR_ENABLE;
587 pcie_write_cmd(slot, slot_cmd);
588
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800589 dbg("%s: SLOTCTRL %x write cmd %x\n",
590 __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_cmd);
rajesh.shah@intel.com1a9ed1b2005-10-31 16:20:10 -0800591 DBG_LEAVE_ROUTINE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 return;
593}
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595static void hpc_release_ctlr(struct controller *ctrl)
596{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 DBG_ENTER_ROUTINE
598
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800599 if (pciehp_poll_mode)
600 del_timer(&ctrl->poll_timer);
601 else
602 free_irq(ctrl->pci_dev->irq, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 DBG_LEAVE_ROUTINE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
607static int hpc_power_on_slot(struct slot * slot)
608{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800609 struct controller *ctrl = slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 u16 slot_cmd;
Rajesh Shah5a49f202005-11-23 15:44:54 -0800611 u16 slot_ctrl, slot_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 int retval = 0;
613
614 DBG_ENTER_ROUTINE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 dbg("%s: slot->hp_slot %x\n", __FUNCTION__, slot->hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Rajesh Shah5a49f202005-11-23 15:44:54 -0800618 /* Clear sticky power-fault bit from previous power failures */
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800619 retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (retval) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800621 err("%s: Cannot read SLOTSTATUS register\n", __FUNCTION__);
622 return retval;
623 }
624 slot_status &= PWR_FAULT_DETECTED;
625 if (slot_status) {
626 retval = pciehp_writew(ctrl, SLOTSTATUS, slot_status);
627 if (retval) {
628 err("%s: Cannot write to SLOTSTATUS register\n",
629 __FUNCTION__);
630 return retval;
631 }
632 }
633
634 retval = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
635 if (retval) {
636 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return retval;
638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 slot_cmd = (slot_ctrl & ~PWR_CTRL) | POWER_ON;
641
Thomas Schaeferc7ab3372005-12-08 11:55:57 -0800642 /* Enable detection that we turned off at slot power-off time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (!pciehp_poll_mode)
Thomas Schaeferc7ab3372005-12-08 11:55:57 -0800644 slot_cmd = slot_cmd |
645 PWR_FAULT_DETECT_ENABLE |
646 MRL_DETECT_ENABLE |
647 PRSN_DETECT_ENABLE |
648 HP_INTR_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 retval = pcie_write_cmd(slot, slot_cmd);
651
652 if (retval) {
653 err("%s: Write %x command failed!\n", __FUNCTION__, slot_cmd);
654 return -1;
655 }
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800656 dbg("%s: SLOTCTRL %x write cmd %x\n",
657 __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 DBG_LEAVE_ROUTINE
660
661 return retval;
662}
663
664static int hpc_power_off_slot(struct slot * slot)
665{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800666 struct controller *ctrl = slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 u16 slot_cmd;
668 u16 slot_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 int retval = 0;
670
671 DBG_ENTER_ROUTINE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 dbg("%s: slot->hp_slot %x\n", __FUNCTION__, slot->hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800675 retval = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (retval) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800677 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return retval;
679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 slot_cmd = (slot_ctrl & ~PWR_CTRL) | POWER_OFF;
682
Thomas Schaeferc7ab3372005-12-08 11:55:57 -0800683 /*
684 * If we get MRL or presence detect interrupts now, the isr
685 * will notice the sticky power-fault bit too and issue power
686 * indicator change commands. This will lead to an endless loop
687 * of command completions, since the power-fault bit remains on
688 * till the slot is powered on again.
689 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (!pciehp_poll_mode)
Thomas Schaeferc7ab3372005-12-08 11:55:57 -0800691 slot_cmd = (slot_cmd &
692 ~PWR_FAULT_DETECT_ENABLE &
693 ~MRL_DETECT_ENABLE &
694 ~PRSN_DETECT_ENABLE) | HP_INTR_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 retval = pcie_write_cmd(slot, slot_cmd);
697
698 if (retval) {
699 err("%s: Write command failed!\n", __FUNCTION__);
700 return -1;
701 }
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800702 dbg("%s: SLOTCTRL %x write cmd %x\n",
703 __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 DBG_LEAVE_ROUTINE
706
707 return retval;
708}
709
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800710static irqreturn_t pcie_isr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800712 struct controller *ctrl = (struct controller *)dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 u16 slot_status, intr_detect, intr_loc;
714 u16 temp_word;
715 int hp_slot = 0; /* only 1 slot per PCI Express port */
716 int rc = 0;
717
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800718 rc = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800720 err("%s: Cannot read SLOTSTATUS register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return IRQ_NONE;
722 }
723
724 intr_detect = ( ATTN_BUTTN_PRESSED | PWR_FAULT_DETECTED | MRL_SENS_CHANGED |
725 PRSN_DETECT_CHANGED | CMD_COMPLETED );
726
727 intr_loc = slot_status & intr_detect;
728
729 /* Check to see if it was our interrupt */
730 if ( !intr_loc )
731 return IRQ_NONE;
732
733 dbg("%s: intr_loc %x\n", __FUNCTION__, intr_loc);
734 /* Mask Hot-plug Interrupt Enable */
735 if (!pciehp_poll_mode) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800736 rc = pciehp_readw(ctrl, SLOTCTRL, &temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800738 err("%s: Cannot read SLOT_CTRL register\n",
739 __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 return IRQ_NONE;
741 }
742
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800743 dbg("%s: pciehp_readw(SLOTCTRL) with value %x\n",
744 __FUNCTION__, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 temp_word = (temp_word & ~HP_INTR_ENABLE & ~CMD_CMPL_INTR_ENABLE) | 0x00;
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800746 rc = pciehp_writew(ctrl, SLOTCTRL, temp_word);
747 if (rc) {
748 err("%s: Cannot write to SLOTCTRL register\n",
749 __FUNCTION__);
750 return IRQ_NONE;
751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800753 rc = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800755 err("%s: Cannot read SLOT_STATUS register\n",
756 __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return IRQ_NONE;
758 }
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800759 dbg("%s: pciehp_readw(SLOTSTATUS) with value %x\n",
760 __FUNCTION__, slot_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 /* Clear command complete interrupt caused by this write */
763 temp_word = 0x1f;
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800764 rc = pciehp_writew(ctrl, SLOTSTATUS, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800766 err("%s: Cannot write to SLOTSTATUS register\n",
767 __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return IRQ_NONE;
769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
771
772 if (intr_loc & CMD_COMPLETED) {
773 /*
774 * Command Complete Interrupt Pending
775 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 wake_up_interruptible(&ctrl->queue);
777 }
778
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800779 if (intr_loc & MRL_SENS_CHANGED)
780 pciehp_handle_switch_change(hp_slot, ctrl);
781
782 if (intr_loc & ATTN_BUTTN_PRESSED)
783 pciehp_handle_attention_button(hp_slot, ctrl);
784
785 if (intr_loc & PRSN_DETECT_CHANGED)
786 pciehp_handle_presence_change(hp_slot, ctrl);
787
788 if (intr_loc & PWR_FAULT_DETECTED)
789 pciehp_handle_power_fault(hp_slot, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 /* Clear all events after serving them */
792 temp_word = 0x1F;
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800793 rc = pciehp_writew(ctrl, SLOTSTATUS, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800795 err("%s: Cannot write to SLOTSTATUS register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return IRQ_NONE;
797 }
798 /* Unmask Hot-plug Interrupt Enable */
799 if (!pciehp_poll_mode) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800800 rc = pciehp_readw(ctrl, SLOTCTRL, &temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800802 err("%s: Cannot read SLOTCTRL register\n",
803 __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 return IRQ_NONE;
805 }
806
807 dbg("%s: Unmask Hot-plug Interrupt Enable\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 temp_word = (temp_word & ~HP_INTR_ENABLE) | HP_INTR_ENABLE;
809
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800810 rc = pciehp_writew(ctrl, SLOTCTRL, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800812 err("%s: Cannot write to SLOTCTRL register\n",
813 __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return IRQ_NONE;
815 }
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800816
817 rc = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800819 err("%s: Cannot read SLOT_STATUS register\n",
820 __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return IRQ_NONE;
822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824 /* Clear command complete interrupt caused by this write */
825 temp_word = 0x1F;
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800826 rc = pciehp_writew(ctrl, SLOTSTATUS, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800828 err("%s: Cannot write to SLOTSTATUS failed\n",
829 __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return IRQ_NONE;
831 }
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800832 dbg("%s: pciehp_writew(SLOTSTATUS) with value %x\n",
833 __FUNCTION__, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
835
836 return IRQ_HANDLED;
837}
838
839static int hpc_get_max_lnk_speed (struct slot *slot, enum pci_bus_speed *value)
840{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800841 struct controller *ctrl = slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 enum pcie_link_speed lnk_speed;
843 u32 lnk_cap;
844 int retval = 0;
845
846 DBG_ENTER_ROUTINE
847
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800848 retval = pciehp_readl(ctrl, LNKCAP, &lnk_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 if (retval) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800850 err("%s: Cannot read LNKCAP register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return retval;
852 }
853
854 switch (lnk_cap & 0x000F) {
855 case 1:
856 lnk_speed = PCIE_2PT5GB;
857 break;
858 default:
859 lnk_speed = PCIE_LNK_SPEED_UNKNOWN;
860 break;
861 }
862
863 *value = lnk_speed;
864 dbg("Max link speed = %d\n", lnk_speed);
865 DBG_LEAVE_ROUTINE
866 return retval;
867}
868
869static int hpc_get_max_lnk_width (struct slot *slot, enum pcie_link_width *value)
870{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800871 struct controller *ctrl = slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 enum pcie_link_width lnk_wdth;
873 u32 lnk_cap;
874 int retval = 0;
875
876 DBG_ENTER_ROUTINE
877
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800878 retval = pciehp_readl(ctrl, LNKCAP, &lnk_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (retval) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800880 err("%s: Cannot read LNKCAP register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return retval;
882 }
883
884 switch ((lnk_cap & 0x03F0) >> 4){
885 case 0:
886 lnk_wdth = PCIE_LNK_WIDTH_RESRV;
887 break;
888 case 1:
889 lnk_wdth = PCIE_LNK_X1;
890 break;
891 case 2:
892 lnk_wdth = PCIE_LNK_X2;
893 break;
894 case 4:
895 lnk_wdth = PCIE_LNK_X4;
896 break;
897 case 8:
898 lnk_wdth = PCIE_LNK_X8;
899 break;
900 case 12:
901 lnk_wdth = PCIE_LNK_X12;
902 break;
903 case 16:
904 lnk_wdth = PCIE_LNK_X16;
905 break;
906 case 32:
907 lnk_wdth = PCIE_LNK_X32;
908 break;
909 default:
910 lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
911 break;
912 }
913
914 *value = lnk_wdth;
915 dbg("Max link width = %d\n", lnk_wdth);
916 DBG_LEAVE_ROUTINE
917 return retval;
918}
919
920static int hpc_get_cur_lnk_speed (struct slot *slot, enum pci_bus_speed *value)
921{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800922 struct controller *ctrl = slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 enum pcie_link_speed lnk_speed = PCI_SPEED_UNKNOWN;
924 int retval = 0;
925 u16 lnk_status;
926
927 DBG_ENTER_ROUTINE
928
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800929 retval = pciehp_readw(ctrl, LNKSTATUS, &lnk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 if (retval) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800931 err("%s: Cannot read LNKSTATUS register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return retval;
933 }
934
935 switch (lnk_status & 0x0F) {
936 case 1:
937 lnk_speed = PCIE_2PT5GB;
938 break;
939 default:
940 lnk_speed = PCIE_LNK_SPEED_UNKNOWN;
941 break;
942 }
943
944 *value = lnk_speed;
945 dbg("Current link speed = %d\n", lnk_speed);
946 DBG_LEAVE_ROUTINE
947 return retval;
948}
949
950static int hpc_get_cur_lnk_width (struct slot *slot, enum pcie_link_width *value)
951{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800952 struct controller *ctrl = slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 enum pcie_link_width lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
954 int retval = 0;
955 u16 lnk_status;
956
957 DBG_ENTER_ROUTINE
958
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800959 retval = pciehp_readw(ctrl, LNKSTATUS, &lnk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 if (retval) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800961 err("%s: Cannot read LNKSTATUS register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 return retval;
963 }
964
965 switch ((lnk_status & 0x03F0) >> 4){
966 case 0:
967 lnk_wdth = PCIE_LNK_WIDTH_RESRV;
968 break;
969 case 1:
970 lnk_wdth = PCIE_LNK_X1;
971 break;
972 case 2:
973 lnk_wdth = PCIE_LNK_X2;
974 break;
975 case 4:
976 lnk_wdth = PCIE_LNK_X4;
977 break;
978 case 8:
979 lnk_wdth = PCIE_LNK_X8;
980 break;
981 case 12:
982 lnk_wdth = PCIE_LNK_X12;
983 break;
984 case 16:
985 lnk_wdth = PCIE_LNK_X16;
986 break;
987 case 32:
988 lnk_wdth = PCIE_LNK_X32;
989 break;
990 default:
991 lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
992 break;
993 }
994
995 *value = lnk_wdth;
996 dbg("Current link width = %d\n", lnk_wdth);
997 DBG_LEAVE_ROUTINE
998 return retval;
999}
1000
1001static struct hpc_ops pciehp_hpc_ops = {
1002 .power_on_slot = hpc_power_on_slot,
1003 .power_off_slot = hpc_power_off_slot,
1004 .set_attention_status = hpc_set_attention_status,
1005 .get_power_status = hpc_get_power_status,
1006 .get_attention_status = hpc_get_attention_status,
1007 .get_latch_status = hpc_get_latch_status,
1008 .get_adapter_status = hpc_get_adapter_status,
1009
1010 .get_max_bus_speed = hpc_get_max_lnk_speed,
1011 .get_cur_bus_speed = hpc_get_cur_lnk_speed,
1012 .get_max_lnk_width = hpc_get_max_lnk_width,
1013 .get_cur_lnk_width = hpc_get_cur_lnk_width,
1014
1015 .query_power_fault = hpc_query_power_fault,
1016 .green_led_on = hpc_set_green_led_on,
1017 .green_led_off = hpc_set_green_led_off,
1018 .green_led_blink = hpc_set_green_led_blink,
1019
1020 .release_ctlr = hpc_release_ctlr,
1021 .check_lnk_status = hpc_check_lnk_status,
1022};
1023
Kristen Accardi783c49f2006-03-03 10:16:05 -08001024#ifdef CONFIG_ACPI
1025int pciehp_acpi_get_hp_hw_control_from_firmware(struct pci_dev *dev)
1026{
1027 acpi_status status;
1028 acpi_handle chandle, handle = DEVICE_ACPI_HANDLE(&(dev->dev));
1029 struct pci_dev *pdev = dev;
1030 struct pci_bus *parent;
MUNEDA Takahirob2e6e3b2006-03-17 09:18:39 +09001031 struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL };
Kristen Accardi783c49f2006-03-03 10:16:05 -08001032
1033 /*
1034 * Per PCI firmware specification, we should run the ACPI _OSC
1035 * method to get control of hotplug hardware before using it.
1036 * If an _OSC is missing, we look for an OSHP to do the same thing.
1037 * To handle different BIOS behavior, we look for _OSC and OSHP
1038 * within the scope of the hotplug controller and its parents, upto
1039 * the host bridge under which this controller exists.
1040 */
1041 while (!handle) {
1042 /*
1043 * This hotplug controller was not listed in the ACPI name
1044 * space at all. Try to get acpi handle of parent pci bus.
1045 */
1046 if (!pdev || !pdev->bus->parent)
1047 break;
1048 parent = pdev->bus->parent;
1049 dbg("Could not find %s in acpi namespace, trying parent\n",
1050 pci_name(pdev));
1051 if (!parent->self)
1052 /* Parent must be a host bridge */
1053 handle = acpi_get_pci_rootbridge_handle(
1054 pci_domain_nr(parent),
1055 parent->number);
1056 else
1057 handle = DEVICE_ACPI_HANDLE(
1058 &(parent->self->dev));
1059 pdev = parent->self;
1060 }
1061
1062 while (handle) {
MUNEDA Takahirob2e6e3b2006-03-17 09:18:39 +09001063 acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
1064 dbg("Trying to get hotplug control for %s \n",
1065 (char *)string.pointer);
Kristen Accardi783c49f2006-03-03 10:16:05 -08001066 status = pci_osc_control_set(handle,
1067 OSC_PCI_EXPRESS_NATIVE_HP_CONTROL);
1068 if (status == AE_NOT_FOUND)
1069 status = acpi_run_oshp(handle);
1070 if (ACPI_SUCCESS(status)) {
1071 dbg("Gained control for hotplug HW for pci %s (%s)\n",
MUNEDA Takahirob2e6e3b2006-03-17 09:18:39 +09001072 pci_name(dev), (char *)string.pointer);
Kristen Accardi81b26bc2006-04-18 14:36:43 -07001073 kfree(string.pointer);
Kristen Accardi783c49f2006-03-03 10:16:05 -08001074 return 0;
1075 }
1076 if (acpi_root_bridge(handle))
1077 break;
1078 chandle = handle;
1079 status = acpi_get_parent(chandle, &handle);
1080 if (ACPI_FAILURE(status))
1081 break;
1082 }
1083
1084 err("Cannot get control of hotplug hardware for pci %s\n",
1085 pci_name(dev));
MUNEDA Takahirob2e6e3b2006-03-17 09:18:39 +09001086
Kristen Accardi81b26bc2006-04-18 14:36:43 -07001087 kfree(string.pointer);
Kristen Accardi783c49f2006-03-03 10:16:05 -08001088 return -1;
1089}
1090#endif
1091
1092
1093
rajesh.shah@intel.comed6cbcf2005-10-31 16:20:09 -08001094int pcie_init(struct controller * ctrl, struct pcie_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 int rc;
1097 static int first = 1;
1098 u16 temp_word;
1099 u16 cap_reg;
1100 u16 intr_enable = 0;
1101 u32 slot_cap;
Kenji Kaneshige75e13172006-12-21 17:01:08 -08001102 int cap_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 u16 slot_status, slot_ctrl;
1104 struct pci_dev *pdev;
1105
1106 DBG_ENTER_ROUTINE
1107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 pdev = dev->port;
Kenji Kaneshige48fe3912006-12-21 17:01:04 -08001109 ctrl->pci_dev = pdev; /* save pci_dev in context */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
rajesh.shah@intel.com1a9ed1b2005-10-31 16:20:10 -08001111 dbg("%s: hotplug controller vendor id 0x%x device id 0x%x\n",
1112 __FUNCTION__, pdev->vendor, pdev->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if ((cap_base = pci_find_capability(pdev, PCI_CAP_ID_EXP)) == 0) {
1115 dbg("%s: Can't find PCI_CAP_ID_EXP (0x10)\n", __FUNCTION__);
1116 goto abort_free_ctlr;
1117 }
1118
Dely Sy8b245e42005-05-06 17:19:09 -07001119 ctrl->cap_base = cap_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Kenji Kaneshige75e13172006-12-21 17:01:08 -08001121 dbg("%s: pcie_cap_base %x\n", __FUNCTION__, cap_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001123 rc = pciehp_readw(ctrl, CAPREG, &cap_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001125 err("%s: Cannot read CAPREG register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 goto abort_free_ctlr;
1127 }
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001128 dbg("%s: CAPREG offset %x cap_reg %x\n",
1129 __FUNCTION__, ctrl->cap_base + CAPREG, cap_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Dely Sy8b245e42005-05-06 17:19:09 -07001131 if (((cap_reg & SLOT_IMPL) == 0) || (((cap_reg & DEV_PORT_TYPE) != 0x0040)
1132 && ((cap_reg & DEV_PORT_TYPE) != 0x0060))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 dbg("%s : This is not a root port or the port is not connected to a slot\n", __FUNCTION__);
1134 goto abort_free_ctlr;
1135 }
1136
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001137 rc = pciehp_readl(ctrl, SLOTCAP, &slot_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001139 err("%s: Cannot read SLOTCAP register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 goto abort_free_ctlr;
1141 }
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001142 dbg("%s: SLOTCAP offset %x slot_cap %x\n",
1143 __FUNCTION__, ctrl->cap_base + SLOTCAP, slot_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
1145 if (!(slot_cap & HP_CAP)) {
1146 dbg("%s : This slot is not hot-plug capable\n", __FUNCTION__);
1147 goto abort_free_ctlr;
1148 }
1149 /* For debugging purpose */
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001150 rc = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001152 err("%s: Cannot read SLOTSTATUS register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 goto abort_free_ctlr;
1154 }
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001155 dbg("%s: SLOTSTATUS offset %x slot_status %x\n",
1156 __FUNCTION__, ctrl->cap_base + SLOTSTATUS, slot_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001158 rc = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001160 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 goto abort_free_ctlr;
1162 }
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001163 dbg("%s: SLOTCTRL offset %x slot_ctrl %x\n",
1164 __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
1166 if (first) {
1167 spin_lock_init(&hpc_event_lock);
1168 first = 0;
1169 }
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 for ( rc = 0; rc < DEVICE_COUNT_RESOURCE; rc++)
1172 if (pci_resource_len(pdev, rc) > 0)
Greg Kroah-Hartman1396a8c2006-06-12 15:14:29 -07001173 dbg("pci resource[%d] start=0x%llx(len=0x%llx)\n", rc,
1174 (unsigned long long)pci_resource_start(pdev, rc),
1175 (unsigned long long)pci_resource_len(pdev, rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 info("HPC vendor_id %x device_id %x ss_vid %x ss_did %x\n", pdev->vendor, pdev->device,
1178 pdev->subsystem_vendor, pdev->subsystem_device);
1179
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001180 mutex_init(&ctrl->crit_sect);
Kenji Kaneshigedd5619c2006-09-22 10:17:29 -07001181 mutex_init(&ctrl->ctrl_lock);
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 /* setup wait queue */
1184 init_waitqueue_head(&ctrl->queue);
1185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 /* return PCI Controller Info */
Kenji Kaneshige48fe3912006-12-21 17:01:04 -08001187 ctrl->slot_device_offset = 0;
1188 ctrl->num_slots = 1;
1189 ctrl->first_slot = slot_cap >> 19;
1190 ctrl->ctrlcap = slot_cap & 0x0000007f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 /* Mask Hot-plug Interrupt Enable */
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001193 rc = pciehp_readw(ctrl, SLOTCTRL, &temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001195 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 goto abort_free_ctlr;
1197 }
1198
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001199 dbg("%s: SLOTCTRL %x value read %x\n",
1200 __FUNCTION__, ctrl->cap_base + SLOTCTRL, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 temp_word = (temp_word & ~HP_INTR_ENABLE & ~CMD_CMPL_INTR_ENABLE) | 0x00;
1202
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001203 rc = pciehp_writew(ctrl, SLOTCTRL, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001205 err("%s: Cannot write to SLOTCTRL register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 goto abort_free_ctlr;
1207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001209 rc = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001211 err("%s: Cannot read SLOTSTATUS register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 goto abort_free_ctlr;
1213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 temp_word = 0x1F; /* Clear all events */
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001216 rc = pciehp_writew(ctrl, SLOTSTATUS, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001218 err("%s: Cannot write to SLOTSTATUS register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 goto abort_free_ctlr;
1220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Kenji Kaneshige48fe3912006-12-21 17:01:04 -08001222 if (pciehp_poll_mode) {
1223 /* Install interrupt polling timer. Start with 10 sec delay */
1224 init_timer(&ctrl->poll_timer);
1225 start_int_poll_timer(ctrl, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 } else {
1227 /* Installs the interrupt handler */
Kenji Kaneshige48fe3912006-12-21 17:01:04 -08001228 rc = request_irq(ctrl->pci_dev->irq, pcie_isr, IRQF_SHARED,
1229 MY_NAME, (void *)ctrl);
1230 dbg("%s: request_irq %d for hpc%d (returns %d)\n",
1231 __FUNCTION__, ctrl->pci_dev->irq, ctlr_seq_num, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 if (rc) {
Kenji Kaneshige48fe3912006-12-21 17:01:04 -08001233 err("Can't get irq %d for the hotplug controller\n",
1234 ctrl->pci_dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 goto abort_free_ctlr;
1236 }
1237 }
rajesh.shah@intel.com1a9ed1b2005-10-31 16:20:10 -08001238 dbg("pciehp ctrl b:d:f:irq=0x%x:%x:%x:%x\n", pdev->bus->number,
1239 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn), dev->irq);
1240
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001241 rc = pciehp_readw(ctrl, SLOTCTRL, &temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001243 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
Jan Beulich9c64f972006-05-09 00:50:31 -07001244 goto abort_free_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 intr_enable = intr_enable | PRSN_DETECT_ENABLE;
1248
1249 if (ATTN_BUTTN(slot_cap))
1250 intr_enable = intr_enable | ATTN_BUTTN_ENABLE;
1251
1252 if (POWER_CTRL(slot_cap))
1253 intr_enable = intr_enable | PWR_FAULT_DETECT_ENABLE;
1254
1255 if (MRL_SENS(slot_cap))
1256 intr_enable = intr_enable | MRL_DETECT_ENABLE;
1257
1258 temp_word = (temp_word & ~intr_enable) | intr_enable;
1259
1260 if (pciehp_poll_mode) {
1261 temp_word = (temp_word & ~HP_INTR_ENABLE) | 0x0;
1262 } else {
1263 temp_word = (temp_word & ~HP_INTR_ENABLE) | HP_INTR_ENABLE;
1264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266 /* Unmask Hot-plug Interrupt Enable for the interrupt notification mechanism case */
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001267 rc = pciehp_writew(ctrl, SLOTCTRL, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001269 err("%s: Cannot write to SLOTCTRL register\n", __FUNCTION__);
Jan Beulich9c64f972006-05-09 00:50:31 -07001270 goto abort_free_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 }
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001272 rc = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001274 err("%s: Cannot read SLOTSTATUS register\n", __FUNCTION__);
Jan Beulich9c64f972006-05-09 00:50:31 -07001275 goto abort_disable_intr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278 temp_word = 0x1F; /* Clear all events */
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001279 rc = pciehp_writew(ctrl, SLOTSTATUS, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001281 err("%s: Cannot write to SLOTSTATUS register\n", __FUNCTION__);
Jan Beulich9c64f972006-05-09 00:50:31 -07001282 goto abort_disable_intr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
rajesh.shah@intel.coma3a45ec2005-10-31 16:20:12 -08001285 if (pciehp_force) {
1286 dbg("Bypassing BIOS check for pciehp use on %s\n",
1287 pci_name(ctrl->pci_dev));
1288 } else {
Rajesh Shah6560aa52005-11-07 13:37:36 -08001289 rc = pciehp_get_hp_hw_control_from_firmware(ctrl->pci_dev);
rajesh.shah@intel.coma3a45ec2005-10-31 16:20:12 -08001290 if (rc)
Jan Beulich9c64f972006-05-09 00:50:31 -07001291 goto abort_disable_intr;
rajesh.shah@intel.coma3a45ec2005-10-31 16:20:12 -08001292 }
rajesh.shah@intel.coma8a2be92005-10-31 16:20:07 -08001293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 ctlr_seq_num++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 ctrl->hpc_ops = &pciehp_hpc_ops;
1296
1297 DBG_LEAVE_ROUTINE
1298 return 0;
1299
1300 /* We end up here for the many possible ways to fail this API. */
Jan Beulich9c64f972006-05-09 00:50:31 -07001301abort_disable_intr:
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001302 rc = pciehp_readw(ctrl, SLOTCTRL, &temp_word);
Jan Beulich9c64f972006-05-09 00:50:31 -07001303 if (!rc) {
1304 temp_word &= ~(intr_enable | HP_INTR_ENABLE);
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001305 rc = pciehp_writew(ctrl, SLOTCTRL, temp_word);
Jan Beulich9c64f972006-05-09 00:50:31 -07001306 }
1307 if (rc)
1308 err("%s : disabling interrupts failed\n", __FUNCTION__);
1309
1310abort_free_irq:
1311 if (pciehp_poll_mode)
Kenji Kaneshige48fe3912006-12-21 17:01:04 -08001312 del_timer_sync(&ctrl->poll_timer);
Jan Beulich9c64f972006-05-09 00:50:31 -07001313 else
Kenji Kaneshige48fe3912006-12-21 17:01:04 -08001314 free_irq(ctrl->pci_dev->irq, ctrl);
Jan Beulich9c64f972006-05-09 00:50:31 -07001315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316abort_free_ctlr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 DBG_LEAVE_ROUTINE
1318 return -1;
1319}