blob: 9aac6a87eb5393f01bc1c7e15956d1efdbb9b5b9 [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>
Kristen Carlson Accardi34d03412007-01-09 13:02:36 -080038#include <linux/time.h>
Andrew Morton5d1b8c92005-11-13 16:06:39 -080039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "../pci.h"
41#include "pciehp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#ifdef DEBUG
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
51
52#define DEFINE_DBG_BUFFER char __dbg_str_buf[256];
53
54#define DBG_PRINT( dbg_flags, args... ) \
55 do { \
56 if ( DEBUG_LEVEL & ( dbg_flags ) ) \
57 { \
58 int len; \
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 ); \
63 } \
64 } while (0)
65
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]");
68#else
69#define DEFINE_DBG_BUFFER
70#define DBG_ENTER_ROUTINE
71#define DBG_LEAVE_ROUTINE
72#endif /* DEBUG */
73
Kenji Kaneshige5d386e12007-03-06 15:02:26 -080074static atomic_t pciehp_num_controllers = ATOMIC_INIT(0);
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076struct ctrl_reg {
77 u8 cap_id;
78 u8 nxt_ptr;
79 u16 cap_reg;
80 u32 dev_cap;
81 u16 dev_ctrl;
82 u16 dev_status;
83 u32 lnk_cap;
84 u16 lnk_ctrl;
85 u16 lnk_status;
86 u32 slot_cap;
87 u16 slot_ctrl;
88 u16 slot_status;
89 u16 root_ctrl;
90 u16 rsvp;
91 u32 root_status;
92} __attribute__ ((packed));
93
94/* offsets to the controller registers based on the above structure layout */
95enum ctrl_offsets {
96 PCIECAPID = offsetof(struct ctrl_reg, cap_id),
97 NXTCAPPTR = offsetof(struct ctrl_reg, nxt_ptr),
98 CAPREG = offsetof(struct ctrl_reg, cap_reg),
99 DEVCAP = offsetof(struct ctrl_reg, dev_cap),
100 DEVCTRL = offsetof(struct ctrl_reg, dev_ctrl),
101 DEVSTATUS = offsetof(struct ctrl_reg, dev_status),
102 LNKCAP = offsetof(struct ctrl_reg, lnk_cap),
103 LNKCTRL = offsetof(struct ctrl_reg, lnk_ctrl),
104 LNKSTATUS = offsetof(struct ctrl_reg, lnk_status),
105 SLOTCAP = offsetof(struct ctrl_reg, slot_cap),
106 SLOTCTRL = offsetof(struct ctrl_reg, slot_ctrl),
107 SLOTSTATUS = offsetof(struct ctrl_reg, slot_status),
108 ROOTCTRL = offsetof(struct ctrl_reg, root_ctrl),
109 ROOTSTATUS = offsetof(struct ctrl_reg, root_status),
110};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800112static inline int pciehp_readw(struct controller *ctrl, int reg, u16 *value)
113{
114 struct pci_dev *dev = ctrl->pci_dev;
115 return pci_read_config_word(dev, ctrl->cap_base + reg, value);
116}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800118static inline int pciehp_readl(struct controller *ctrl, int reg, u32 *value)
119{
120 struct pci_dev *dev = ctrl->pci_dev;
121 return pci_read_config_dword(dev, ctrl->cap_base + reg, value);
122}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800124static inline int pciehp_writew(struct controller *ctrl, int reg, u16 value)
125{
126 struct pci_dev *dev = ctrl->pci_dev;
127 return pci_write_config_word(dev, ctrl->cap_base + reg, value);
128}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800130static inline int pciehp_writel(struct controller *ctrl, int reg, u32 value)
131{
132 struct pci_dev *dev = ctrl->pci_dev;
133 return pci_write_config_dword(dev, ctrl->cap_base + reg, value);
134}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136/* Field definitions in PCI Express Capabilities Register */
137#define CAP_VER 0x000F
138#define DEV_PORT_TYPE 0x00F0
139#define SLOT_IMPL 0x0100
140#define MSG_NUM 0x3E00
141
142/* Device or Port Type */
143#define NAT_ENDPT 0x00
144#define LEG_ENDPT 0x01
145#define ROOT_PORT 0x04
146#define UP_STREAM 0x05
147#define DN_STREAM 0x06
148#define PCIE_PCI_BRDG 0x07
149#define PCI_PCIE_BRDG 0x10
150
151/* Field definitions in Device Capabilities Register */
152#define DATTN_BUTTN_PRSN 0x1000
153#define DATTN_LED_PRSN 0x2000
154#define DPWR_LED_PRSN 0x4000
155
156/* Field definitions in Link Capabilities Register */
157#define MAX_LNK_SPEED 0x000F
158#define MAX_LNK_WIDTH 0x03F0
159
160/* Link Width Encoding */
161#define LNK_X1 0x01
162#define LNK_X2 0x02
163#define LNK_X4 0x04
164#define LNK_X8 0x08
165#define LNK_X12 0x0C
166#define LNK_X16 0x10
167#define LNK_X32 0x20
168
169/*Field definitions of Link Status Register */
170#define LNK_SPEED 0x000F
171#define NEG_LINK_WD 0x03F0
172#define LNK_TRN_ERR 0x0400
173#define LNK_TRN 0x0800
174#define SLOT_CLK_CONF 0x1000
175
176/* Field definitions in Slot Capabilities Register */
177#define ATTN_BUTTN_PRSN 0x00000001
178#define PWR_CTRL_PRSN 0x00000002
179#define MRL_SENS_PRSN 0x00000004
180#define ATTN_LED_PRSN 0x00000008
181#define PWR_LED_PRSN 0x00000010
182#define HP_SUPR_RM_SUP 0x00000020
183#define HP_CAP 0x00000040
184#define SLOT_PWR_VALUE 0x000003F8
185#define SLOT_PWR_LIMIT 0x00000C00
186#define PSN 0xFFF80000 /* PSN: Physical Slot Number */
187
188/* Field definitions in Slot Control Register */
189#define ATTN_BUTTN_ENABLE 0x0001
190#define PWR_FAULT_DETECT_ENABLE 0x0002
191#define MRL_DETECT_ENABLE 0x0004
192#define PRSN_DETECT_ENABLE 0x0008
193#define CMD_CMPL_INTR_ENABLE 0x0010
194#define HP_INTR_ENABLE 0x0020
195#define ATTN_LED_CTRL 0x00C0
196#define PWR_LED_CTRL 0x0300
197#define PWR_CTRL 0x0400
Kristen Carlson Accardi34d03412007-01-09 13:02:36 -0800198#define EMI_CTRL 0x0800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200/* Attention indicator and Power indicator states */
201#define LED_ON 0x01
202#define LED_BLINK 0x10
203#define LED_OFF 0x11
204
205/* Power Control Command */
206#define POWER_ON 0
207#define POWER_OFF 0x0400
208
Kristen Carlson Accardi34d03412007-01-09 13:02:36 -0800209/* EMI Status defines */
210#define EMI_DISENGAGED 0
211#define EMI_ENGAGED 1
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213/* Field definitions in Slot Status Register */
214#define ATTN_BUTTN_PRESSED 0x0001
215#define PWR_FAULT_DETECTED 0x0002
216#define MRL_SENS_CHANGED 0x0004
217#define PRSN_DETECT_CHANGED 0x0008
218#define CMD_COMPLETED 0x0010
219#define MRL_STATE 0x0020
220#define PRSN_STATE 0x0040
Kristen Carlson Accardi34d03412007-01-09 13:02:36 -0800221#define EMI_STATE 0x0080
222#define EMI_STATUS_BIT 7
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224DEFINE_DBG_BUFFER /* Debug string buffer for entire HPC defined here */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800226static irqreturn_t pcie_isr(int irq, void *dev_id);
227static void start_int_poll_timer(struct controller *ctrl, int sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229/* This is the interrupt polling timeout function. */
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800230static void int_poll_timeout(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800232 struct controller *ctrl = (struct controller *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 DBG_ENTER_ROUTINE
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 /* Poll for interrupt events. regs == NULL => polling */
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800237 pcie_isr(0, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800239 init_timer(&ctrl->poll_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (!pciehp_poll_time)
241 pciehp_poll_time = 2; /* reset timer to poll in 2 secs if user doesn't specify at module installation*/
242
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800243 start_int_poll_timer(ctrl, pciehp_poll_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
246/* This function starts the interrupt polling timer. */
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800247static void start_int_poll_timer(struct controller *ctrl, int sec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800249 /* Clamp to sane value */
250 if ((sec <= 0) || (sec > 60))
251 sec = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800253 ctrl->poll_timer.function = &int_poll_timeout;
254 ctrl->poll_timer.data = (unsigned long)ctrl;
255 ctrl->poll_timer.expires = jiffies + sec * HZ;
256 add_timer(&ctrl->poll_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800259static inline int pcie_wait_cmd(struct controller *ctrl)
260{
Kenji Kaneshige262303fe2006-12-21 17:01:10 -0800261 int retval = 0;
262 unsigned int msecs = pciehp_poll_mode ? 2500 : 1000;
263 unsigned long timeout = msecs_to_jiffies(msecs);
264 int rc;
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800265
Kenji Kaneshige262303fe2006-12-21 17:01:10 -0800266 rc = wait_event_interruptible_timeout(ctrl->queue,
267 !ctrl->cmd_busy, timeout);
268 if (!rc)
269 dbg("Command not completed in 1000 msec\n");
270 else if (rc < 0) {
271 retval = -EINTR;
272 info("Command was interrupted by a signal\n");
273 }
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800274
Kenji Kaneshige262303fe2006-12-21 17:01:10 -0800275 return retval;
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800276}
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278static int pcie_write_cmd(struct slot *slot, u16 cmd)
279{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800280 struct controller *ctrl = slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 int retval = 0;
282 u16 slot_status;
283
284 DBG_ENTER_ROUTINE
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800285
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800286 mutex_lock(&ctrl->ctrl_lock);
287
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800288 retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if (retval) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800290 err("%s: Cannot read SLOTSTATUS register\n", __FUNCTION__);
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800291 goto out;
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800292 }
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 if ((slot_status & CMD_COMPLETED) == CMD_COMPLETED ) {
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800295 /* After 1 sec and CMD_COMPLETED still not set, just
296 proceed forward to issue the next command according
297 to spec. Just print out the error message */
298 dbg("%s: CMD_COMPLETED not clear after 1 sec.\n",
299 __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
301
Kenji Kaneshige262303fe2006-12-21 17:01:10 -0800302 ctrl->cmd_busy = 1;
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800303 retval = pciehp_writew(ctrl, SLOTCTRL, (cmd | CMD_CMPL_INTR_ENABLE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 if (retval) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800305 err("%s: Cannot write to SLOTCTRL register\n", __FUNCTION__);
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800306 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800309 /*
310 * Wait for command completion.
311 */
312 retval = pcie_wait_cmd(ctrl);
313 out:
314 mutex_unlock(&ctrl->ctrl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 DBG_LEAVE_ROUTINE
316 return retval;
317}
318
319static int hpc_check_lnk_status(struct controller *ctrl)
320{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 u16 lnk_status;
322 int retval = 0;
323
324 DBG_ENTER_ROUTINE
325
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800326 retval = pciehp_readw(ctrl, LNKSTATUS, &lnk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if (retval) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800328 err("%s: Cannot read LNKSTATUS register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return retval;
330 }
331
332 dbg("%s: lnk_status = %x\n", __FUNCTION__, lnk_status);
333 if ( (lnk_status & LNK_TRN) || (lnk_status & LNK_TRN_ERR) ||
334 !(lnk_status & NEG_LINK_WD)) {
335 err("%s : Link Training Error occurs \n", __FUNCTION__);
336 retval = -1;
337 return retval;
338 }
339
340 DBG_LEAVE_ROUTINE
341 return retval;
342}
343
344
345static int hpc_get_attention_status(struct slot *slot, u8 *status)
346{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800347 struct controller *ctrl = slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 u16 slot_ctrl;
349 u8 atten_led_state;
350 int retval = 0;
351
352 DBG_ENTER_ROUTINE
353
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800354 retval = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (retval) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800356 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return retval;
358 }
359
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800360 dbg("%s: SLOTCTRL %x, value read %x\n",
361 __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 atten_led_state = (slot_ctrl & ATTN_LED_CTRL) >> 6;
364
365 switch (atten_led_state) {
366 case 0:
367 *status = 0xFF; /* Reserved */
368 break;
369 case 1:
370 *status = 1; /* On */
371 break;
372 case 2:
373 *status = 2; /* Blink */
374 break;
375 case 3:
376 *status = 0; /* Off */
377 break;
378 default:
379 *status = 0xFF;
380 break;
381 }
382
383 DBG_LEAVE_ROUTINE
384 return 0;
385}
386
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800387static int hpc_get_power_status(struct slot *slot, u8 *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800389 struct controller *ctrl = slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 u16 slot_ctrl;
391 u8 pwr_state;
392 int retval = 0;
393
394 DBG_ENTER_ROUTINE
395
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800396 retval = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (retval) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800398 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return retval;
400 }
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800401 dbg("%s: SLOTCTRL %x value read %x\n",
402 __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 pwr_state = (slot_ctrl & PWR_CTRL) >> 10;
405
406 switch (pwr_state) {
407 case 0:
408 *status = 1;
409 break;
410 case 1:
411 *status = 0;
412 break;
413 default:
414 *status = 0xFF;
415 break;
416 }
417
418 DBG_LEAVE_ROUTINE
419 return retval;
420}
421
422
423static int hpc_get_latch_status(struct slot *slot, u8 *status)
424{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800425 struct controller *ctrl = slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 u16 slot_status;
427 int retval = 0;
428
429 DBG_ENTER_ROUTINE
430
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800431 retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (retval) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800433 err("%s: Cannot read SLOTSTATUS register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return retval;
435 }
436
437 *status = (((slot_status & MRL_STATE) >> 5) == 0) ? 0 : 1;
438
439 DBG_LEAVE_ROUTINE
440 return 0;
441}
442
443static int hpc_get_adapter_status(struct slot *slot, u8 *status)
444{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800445 struct controller *ctrl = slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 u16 slot_status;
447 u8 card_state;
448 int retval = 0;
449
450 DBG_ENTER_ROUTINE
451
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800452 retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (retval) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800454 err("%s: Cannot read SLOTSTATUS register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return retval;
456 }
457 card_state = (u8)((slot_status & PRSN_STATE) >> 6);
458 *status = (card_state == 1) ? 1 : 0;
459
460 DBG_LEAVE_ROUTINE
461 return 0;
462}
463
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800464static int hpc_query_power_fault(struct slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800466 struct controller *ctrl = slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 u16 slot_status;
468 u8 pwr_fault;
469 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 DBG_ENTER_ROUTINE
472
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800473 retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (retval) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800475 err("%s: Cannot check for power fault\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return retval;
477 }
478 pwr_fault = (u8)((slot_status & PWR_FAULT_DETECTED) >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 DBG_LEAVE_ROUTINE
rajesh.shah@intel.com8239def2005-10-31 16:20:13 -0800481 return pwr_fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
Kristen Carlson Accardi34d03412007-01-09 13:02:36 -0800484static int hpc_get_emi_status(struct slot *slot, u8 *status)
485{
486 struct controller *ctrl = slot->ctrl;
487 u16 slot_status;
488 int retval = 0;
489
490 DBG_ENTER_ROUTINE
491
492 retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
493 if (retval) {
494 err("%s : Cannot check EMI status\n", __FUNCTION__);
495 return retval;
496 }
497 *status = (slot_status & EMI_STATE) >> EMI_STATUS_BIT;
498
499 DBG_LEAVE_ROUTINE
500 return retval;
501}
502
503static int hpc_toggle_emi(struct slot *slot)
504{
505 struct controller *ctrl = slot->ctrl;
506 u16 slot_cmd = 0;
507 u16 slot_ctrl;
508 int rc = 0;
509
510 DBG_ENTER_ROUTINE
511
512 rc = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
513 if (rc) {
514 err("%s : hp_register_read_word SLOT_CTRL failed\n",
515 __FUNCTION__);
516 return rc;
517 }
518
519 slot_cmd = (slot_ctrl | EMI_CTRL);
520 if (!pciehp_poll_mode)
521 slot_cmd = slot_cmd | HP_INTR_ENABLE;
522
523 pcie_write_cmd(slot, slot_cmd);
524 slot->last_emi_toggle = get_seconds();
525 DBG_LEAVE_ROUTINE
526 return rc;
527}
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529static int hpc_set_attention_status(struct slot *slot, u8 value)
530{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800531 struct controller *ctrl = slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 u16 slot_cmd = 0;
533 u16 slot_ctrl;
534 int rc = 0;
535
rajesh.shah@intel.com1a9ed1b2005-10-31 16:20:10 -0800536 DBG_ENTER_ROUTINE
537
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800538 rc = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800540 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return rc;
542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 switch (value) {
545 case 0 : /* turn off */
546 slot_cmd = (slot_ctrl & ~ATTN_LED_CTRL) | 0x00C0;
547 break;
548 case 1: /* turn on */
549 slot_cmd = (slot_ctrl & ~ATTN_LED_CTRL) | 0x0040;
550 break;
551 case 2: /* turn blink */
552 slot_cmd = (slot_ctrl & ~ATTN_LED_CTRL) | 0x0080;
553 break;
554 default:
555 return -1;
556 }
557 if (!pciehp_poll_mode)
558 slot_cmd = slot_cmd | HP_INTR_ENABLE;
559
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 rc;
566}
567
568
569static void hpc_set_green_led_on(struct slot *slot)
570{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800571 struct controller *ctrl = slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 u16 slot_cmd;
573 u16 slot_ctrl;
574 int rc = 0;
575
rajesh.shah@intel.com1a9ed1b2005-10-31 16:20:10 -0800576 DBG_ENTER_ROUTINE
577
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800578 rc = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800580 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return;
582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 slot_cmd = (slot_ctrl & ~PWR_LED_CTRL) | 0x0100;
584 if (!pciehp_poll_mode)
585 slot_cmd = slot_cmd | HP_INTR_ENABLE;
586
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
595static void hpc_set_green_led_off(struct slot *slot)
596{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800597 struct controller *ctrl = slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 u16 slot_cmd;
599 u16 slot_ctrl;
600 int rc = 0;
601
rajesh.shah@intel.com1a9ed1b2005-10-31 16:20:10 -0800602 DBG_ENTER_ROUTINE
603
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800604 rc = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800606 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 return;
608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 slot_cmd = (slot_ctrl & ~PWR_LED_CTRL) | 0x0300;
611
612 if (!pciehp_poll_mode)
613 slot_cmd = slot_cmd | HP_INTR_ENABLE;
614 pcie_write_cmd(slot, slot_cmd);
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800615 dbg("%s: SLOTCTRL %x write cmd %x\n",
616 __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
rajesh.shah@intel.com1a9ed1b2005-10-31 16:20:10 -0800618 DBG_LEAVE_ROUTINE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return;
620}
621
622static void hpc_set_green_led_blink(struct slot *slot)
623{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800624 struct controller *ctrl = slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 u16 slot_cmd;
626 u16 slot_ctrl;
627 int rc = 0;
628
rajesh.shah@intel.com1a9ed1b2005-10-31 16:20:10 -0800629 DBG_ENTER_ROUTINE
630
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800631 rc = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800633 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return;
635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 slot_cmd = (slot_ctrl & ~PWR_LED_CTRL) | 0x0200;
638
639 if (!pciehp_poll_mode)
640 slot_cmd = slot_cmd | HP_INTR_ENABLE;
641 pcie_write_cmd(slot, slot_cmd);
642
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800643 dbg("%s: SLOTCTRL %x write cmd %x\n",
644 __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_cmd);
rajesh.shah@intel.com1a9ed1b2005-10-31 16:20:10 -0800645 DBG_LEAVE_ROUTINE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return;
647}
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649static void hpc_release_ctlr(struct controller *ctrl)
650{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 DBG_ENTER_ROUTINE
652
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800653 if (pciehp_poll_mode)
654 del_timer(&ctrl->poll_timer);
655 else
656 free_irq(ctrl->pci_dev->irq, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800658 /*
659 * If this is the last controller to be released, destroy the
660 * pciehp work queue
661 */
662 if (atomic_dec_and_test(&pciehp_num_controllers))
663 destroy_workqueue(pciehp_wq);
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 DBG_LEAVE_ROUTINE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
668static int hpc_power_on_slot(struct slot * slot)
669{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800670 struct controller *ctrl = slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 u16 slot_cmd;
Rajesh Shah5a49f202005-11-23 15:44:54 -0800672 u16 slot_ctrl, slot_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 int retval = 0;
674
675 DBG_ENTER_ROUTINE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 dbg("%s: slot->hp_slot %x\n", __FUNCTION__, slot->hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Rajesh Shah5a49f202005-11-23 15:44:54 -0800679 /* Clear sticky power-fault bit from previous power failures */
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800680 retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (retval) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800682 err("%s: Cannot read SLOTSTATUS register\n", __FUNCTION__);
683 return retval;
684 }
685 slot_status &= PWR_FAULT_DETECTED;
686 if (slot_status) {
687 retval = pciehp_writew(ctrl, SLOTSTATUS, slot_status);
688 if (retval) {
689 err("%s: Cannot write to SLOTSTATUS register\n",
690 __FUNCTION__);
691 return retval;
692 }
693 }
694
695 retval = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
696 if (retval) {
697 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 return retval;
699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 slot_cmd = (slot_ctrl & ~PWR_CTRL) | POWER_ON;
702
Thomas Schaeferc7ab3372005-12-08 11:55:57 -0800703 /* Enable detection that we turned off at slot power-off time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (!pciehp_poll_mode)
Thomas Schaeferc7ab3372005-12-08 11:55:57 -0800705 slot_cmd = slot_cmd |
706 PWR_FAULT_DETECT_ENABLE |
707 MRL_DETECT_ENABLE |
708 PRSN_DETECT_ENABLE |
709 HP_INTR_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 retval = pcie_write_cmd(slot, slot_cmd);
712
713 if (retval) {
714 err("%s: Write %x command failed!\n", __FUNCTION__, slot_cmd);
715 return -1;
716 }
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800717 dbg("%s: SLOTCTRL %x write cmd %x\n",
718 __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 DBG_LEAVE_ROUTINE
721
722 return retval;
723}
724
725static int hpc_power_off_slot(struct slot * slot)
726{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800727 struct controller *ctrl = slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 u16 slot_cmd;
729 u16 slot_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 int retval = 0;
731
732 DBG_ENTER_ROUTINE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 dbg("%s: slot->hp_slot %x\n", __FUNCTION__, slot->hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800736 retval = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (retval) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800738 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 return retval;
740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742 slot_cmd = (slot_ctrl & ~PWR_CTRL) | POWER_OFF;
743
Thomas Schaeferc7ab3372005-12-08 11:55:57 -0800744 /*
745 * If we get MRL or presence detect interrupts now, the isr
746 * will notice the sticky power-fault bit too and issue power
747 * indicator change commands. This will lead to an endless loop
748 * of command completions, since the power-fault bit remains on
749 * till the slot is powered on again.
750 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (!pciehp_poll_mode)
Thomas Schaeferc7ab3372005-12-08 11:55:57 -0800752 slot_cmd = (slot_cmd &
753 ~PWR_FAULT_DETECT_ENABLE &
754 ~MRL_DETECT_ENABLE &
755 ~PRSN_DETECT_ENABLE) | HP_INTR_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 retval = pcie_write_cmd(slot, slot_cmd);
758
759 if (retval) {
760 err("%s: Write command failed!\n", __FUNCTION__);
761 return -1;
762 }
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800763 dbg("%s: SLOTCTRL %x write cmd %x\n",
764 __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 DBG_LEAVE_ROUTINE
767
768 return retval;
769}
770
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800771static irqreturn_t pcie_isr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800773 struct controller *ctrl = (struct controller *)dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 u16 slot_status, intr_detect, intr_loc;
775 u16 temp_word;
776 int hp_slot = 0; /* only 1 slot per PCI Express port */
777 int rc = 0;
778
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800779 rc = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800781 err("%s: Cannot read SLOTSTATUS register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return IRQ_NONE;
783 }
784
785 intr_detect = ( ATTN_BUTTN_PRESSED | PWR_FAULT_DETECTED | MRL_SENS_CHANGED |
786 PRSN_DETECT_CHANGED | CMD_COMPLETED );
787
788 intr_loc = slot_status & intr_detect;
789
790 /* Check to see if it was our interrupt */
791 if ( !intr_loc )
792 return IRQ_NONE;
793
794 dbg("%s: intr_loc %x\n", __FUNCTION__, intr_loc);
795 /* Mask Hot-plug Interrupt Enable */
796 if (!pciehp_poll_mode) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800797 rc = pciehp_readw(ctrl, SLOTCTRL, &temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800799 err("%s: Cannot read SLOT_CTRL register\n",
800 __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return IRQ_NONE;
802 }
803
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800804 dbg("%s: pciehp_readw(SLOTCTRL) with value %x\n",
805 __FUNCTION__, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 temp_word = (temp_word & ~HP_INTR_ENABLE & ~CMD_CMPL_INTR_ENABLE) | 0x00;
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800807 rc = pciehp_writew(ctrl, SLOTCTRL, temp_word);
808 if (rc) {
809 err("%s: Cannot write to SLOTCTRL register\n",
810 __FUNCTION__);
811 return IRQ_NONE;
812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800814 rc = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800816 err("%s: Cannot read SLOT_STATUS register\n",
817 __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 return IRQ_NONE;
819 }
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800820 dbg("%s: pciehp_readw(SLOTSTATUS) with value %x\n",
821 __FUNCTION__, slot_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 /* Clear command complete interrupt caused by this write */
824 temp_word = 0x1f;
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800825 rc = pciehp_writew(ctrl, SLOTSTATUS, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800827 err("%s: Cannot write to SLOTSTATUS register\n",
828 __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return IRQ_NONE;
830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
832
833 if (intr_loc & CMD_COMPLETED) {
834 /*
835 * Command Complete Interrupt Pending
836 */
Kenji Kaneshige262303fe2006-12-21 17:01:10 -0800837 ctrl->cmd_busy = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 wake_up_interruptible(&ctrl->queue);
839 }
840
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800841 if (intr_loc & MRL_SENS_CHANGED)
842 pciehp_handle_switch_change(hp_slot, ctrl);
843
844 if (intr_loc & ATTN_BUTTN_PRESSED)
845 pciehp_handle_attention_button(hp_slot, ctrl);
846
847 if (intr_loc & PRSN_DETECT_CHANGED)
848 pciehp_handle_presence_change(hp_slot, ctrl);
849
850 if (intr_loc & PWR_FAULT_DETECTED)
851 pciehp_handle_power_fault(hp_slot, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 /* Clear all events after serving them */
854 temp_word = 0x1F;
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800855 rc = pciehp_writew(ctrl, SLOTSTATUS, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800857 err("%s: Cannot write to SLOTSTATUS register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return IRQ_NONE;
859 }
860 /* Unmask Hot-plug Interrupt Enable */
861 if (!pciehp_poll_mode) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800862 rc = pciehp_readw(ctrl, SLOTCTRL, &temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800864 err("%s: Cannot read SLOTCTRL register\n",
865 __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return IRQ_NONE;
867 }
868
869 dbg("%s: Unmask Hot-plug Interrupt Enable\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 temp_word = (temp_word & ~HP_INTR_ENABLE) | HP_INTR_ENABLE;
871
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800872 rc = pciehp_writew(ctrl, SLOTCTRL, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800874 err("%s: Cannot write to SLOTCTRL register\n",
875 __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return IRQ_NONE;
877 }
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800878
879 rc = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800881 err("%s: Cannot read SLOT_STATUS register\n",
882 __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return IRQ_NONE;
884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 /* Clear command complete interrupt caused by this write */
887 temp_word = 0x1F;
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800888 rc = pciehp_writew(ctrl, SLOTSTATUS, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800890 err("%s: Cannot write to SLOTSTATUS failed\n",
891 __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return IRQ_NONE;
893 }
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800894 dbg("%s: pciehp_writew(SLOTSTATUS) with value %x\n",
895 __FUNCTION__, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 }
897
898 return IRQ_HANDLED;
899}
900
901static int hpc_get_max_lnk_speed (struct slot *slot, enum pci_bus_speed *value)
902{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800903 struct controller *ctrl = slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 enum pcie_link_speed lnk_speed;
905 u32 lnk_cap;
906 int retval = 0;
907
908 DBG_ENTER_ROUTINE
909
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800910 retval = pciehp_readl(ctrl, LNKCAP, &lnk_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (retval) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800912 err("%s: Cannot read LNKCAP register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 return retval;
914 }
915
916 switch (lnk_cap & 0x000F) {
917 case 1:
918 lnk_speed = PCIE_2PT5GB;
919 break;
920 default:
921 lnk_speed = PCIE_LNK_SPEED_UNKNOWN;
922 break;
923 }
924
925 *value = lnk_speed;
926 dbg("Max link speed = %d\n", lnk_speed);
927 DBG_LEAVE_ROUTINE
928 return retval;
929}
930
931static int hpc_get_max_lnk_width (struct slot *slot, enum pcie_link_width *value)
932{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800933 struct controller *ctrl = slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 enum pcie_link_width lnk_wdth;
935 u32 lnk_cap;
936 int retval = 0;
937
938 DBG_ENTER_ROUTINE
939
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800940 retval = pciehp_readl(ctrl, LNKCAP, &lnk_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 if (retval) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800942 err("%s: Cannot read LNKCAP register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 return retval;
944 }
945
946 switch ((lnk_cap & 0x03F0) >> 4){
947 case 0:
948 lnk_wdth = PCIE_LNK_WIDTH_RESRV;
949 break;
950 case 1:
951 lnk_wdth = PCIE_LNK_X1;
952 break;
953 case 2:
954 lnk_wdth = PCIE_LNK_X2;
955 break;
956 case 4:
957 lnk_wdth = PCIE_LNK_X4;
958 break;
959 case 8:
960 lnk_wdth = PCIE_LNK_X8;
961 break;
962 case 12:
963 lnk_wdth = PCIE_LNK_X12;
964 break;
965 case 16:
966 lnk_wdth = PCIE_LNK_X16;
967 break;
968 case 32:
969 lnk_wdth = PCIE_LNK_X32;
970 break;
971 default:
972 lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
973 break;
974 }
975
976 *value = lnk_wdth;
977 dbg("Max link width = %d\n", lnk_wdth);
978 DBG_LEAVE_ROUTINE
979 return retval;
980}
981
982static int hpc_get_cur_lnk_speed (struct slot *slot, enum pci_bus_speed *value)
983{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800984 struct controller *ctrl = slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 enum pcie_link_speed lnk_speed = PCI_SPEED_UNKNOWN;
986 int retval = 0;
987 u16 lnk_status;
988
989 DBG_ENTER_ROUTINE
990
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800991 retval = pciehp_readw(ctrl, LNKSTATUS, &lnk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 if (retval) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -0800993 err("%s: Cannot read LNKSTATUS register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return retval;
995 }
996
997 switch (lnk_status & 0x0F) {
998 case 1:
999 lnk_speed = PCIE_2PT5GB;
1000 break;
1001 default:
1002 lnk_speed = PCIE_LNK_SPEED_UNKNOWN;
1003 break;
1004 }
1005
1006 *value = lnk_speed;
1007 dbg("Current link speed = %d\n", lnk_speed);
1008 DBG_LEAVE_ROUTINE
1009 return retval;
1010}
1011
1012static int hpc_get_cur_lnk_width (struct slot *slot, enum pcie_link_width *value)
1013{
Kenji Kaneshige48fe3912006-12-21 17:01:04 -08001014 struct controller *ctrl = slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 enum pcie_link_width lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
1016 int retval = 0;
1017 u16 lnk_status;
1018
1019 DBG_ENTER_ROUTINE
1020
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001021 retval = pciehp_readw(ctrl, LNKSTATUS, &lnk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 if (retval) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001023 err("%s: Cannot read LNKSTATUS register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 return retval;
1025 }
1026
1027 switch ((lnk_status & 0x03F0) >> 4){
1028 case 0:
1029 lnk_wdth = PCIE_LNK_WIDTH_RESRV;
1030 break;
1031 case 1:
1032 lnk_wdth = PCIE_LNK_X1;
1033 break;
1034 case 2:
1035 lnk_wdth = PCIE_LNK_X2;
1036 break;
1037 case 4:
1038 lnk_wdth = PCIE_LNK_X4;
1039 break;
1040 case 8:
1041 lnk_wdth = PCIE_LNK_X8;
1042 break;
1043 case 12:
1044 lnk_wdth = PCIE_LNK_X12;
1045 break;
1046 case 16:
1047 lnk_wdth = PCIE_LNK_X16;
1048 break;
1049 case 32:
1050 lnk_wdth = PCIE_LNK_X32;
1051 break;
1052 default:
1053 lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
1054 break;
1055 }
1056
1057 *value = lnk_wdth;
1058 dbg("Current link width = %d\n", lnk_wdth);
1059 DBG_LEAVE_ROUTINE
1060 return retval;
1061}
1062
1063static struct hpc_ops pciehp_hpc_ops = {
1064 .power_on_slot = hpc_power_on_slot,
1065 .power_off_slot = hpc_power_off_slot,
1066 .set_attention_status = hpc_set_attention_status,
1067 .get_power_status = hpc_get_power_status,
1068 .get_attention_status = hpc_get_attention_status,
1069 .get_latch_status = hpc_get_latch_status,
1070 .get_adapter_status = hpc_get_adapter_status,
Kristen Carlson Accardi34d03412007-01-09 13:02:36 -08001071 .get_emi_status = hpc_get_emi_status,
1072 .toggle_emi = hpc_toggle_emi,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074 .get_max_bus_speed = hpc_get_max_lnk_speed,
1075 .get_cur_bus_speed = hpc_get_cur_lnk_speed,
1076 .get_max_lnk_width = hpc_get_max_lnk_width,
1077 .get_cur_lnk_width = hpc_get_cur_lnk_width,
1078
1079 .query_power_fault = hpc_query_power_fault,
1080 .green_led_on = hpc_set_green_led_on,
1081 .green_led_off = hpc_set_green_led_off,
1082 .green_led_blink = hpc_set_green_led_blink,
1083
1084 .release_ctlr = hpc_release_ctlr,
1085 .check_lnk_status = hpc_check_lnk_status,
1086};
1087
Kristen Accardi783c49f2006-03-03 10:16:05 -08001088#ifdef CONFIG_ACPI
1089int pciehp_acpi_get_hp_hw_control_from_firmware(struct pci_dev *dev)
1090{
1091 acpi_status status;
1092 acpi_handle chandle, handle = DEVICE_ACPI_HANDLE(&(dev->dev));
1093 struct pci_dev *pdev = dev;
1094 struct pci_bus *parent;
MUNEDA Takahirob2e6e3b2006-03-17 09:18:39 +09001095 struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL };
Kristen Accardi783c49f2006-03-03 10:16:05 -08001096
1097 /*
1098 * Per PCI firmware specification, we should run the ACPI _OSC
1099 * method to get control of hotplug hardware before using it.
1100 * If an _OSC is missing, we look for an OSHP to do the same thing.
1101 * To handle different BIOS behavior, we look for _OSC and OSHP
1102 * within the scope of the hotplug controller and its parents, upto
1103 * the host bridge under which this controller exists.
1104 */
1105 while (!handle) {
1106 /*
1107 * This hotplug controller was not listed in the ACPI name
1108 * space at all. Try to get acpi handle of parent pci bus.
1109 */
1110 if (!pdev || !pdev->bus->parent)
1111 break;
1112 parent = pdev->bus->parent;
1113 dbg("Could not find %s in acpi namespace, trying parent\n",
1114 pci_name(pdev));
1115 if (!parent->self)
1116 /* Parent must be a host bridge */
1117 handle = acpi_get_pci_rootbridge_handle(
1118 pci_domain_nr(parent),
1119 parent->number);
1120 else
1121 handle = DEVICE_ACPI_HANDLE(
1122 &(parent->self->dev));
1123 pdev = parent->self;
1124 }
1125
1126 while (handle) {
MUNEDA Takahirob2e6e3b2006-03-17 09:18:39 +09001127 acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
1128 dbg("Trying to get hotplug control for %s \n",
1129 (char *)string.pointer);
Kristen Accardi783c49f2006-03-03 10:16:05 -08001130 status = pci_osc_control_set(handle,
1131 OSC_PCI_EXPRESS_NATIVE_HP_CONTROL);
1132 if (status == AE_NOT_FOUND)
1133 status = acpi_run_oshp(handle);
1134 if (ACPI_SUCCESS(status)) {
1135 dbg("Gained control for hotplug HW for pci %s (%s)\n",
MUNEDA Takahirob2e6e3b2006-03-17 09:18:39 +09001136 pci_name(dev), (char *)string.pointer);
Kristen Accardi81b26bc2006-04-18 14:36:43 -07001137 kfree(string.pointer);
Kristen Accardi783c49f2006-03-03 10:16:05 -08001138 return 0;
1139 }
1140 if (acpi_root_bridge(handle))
1141 break;
1142 chandle = handle;
1143 status = acpi_get_parent(chandle, &handle);
1144 if (ACPI_FAILURE(status))
1145 break;
1146 }
1147
1148 err("Cannot get control of hotplug hardware for pci %s\n",
1149 pci_name(dev));
MUNEDA Takahirob2e6e3b2006-03-17 09:18:39 +09001150
Kristen Accardi81b26bc2006-04-18 14:36:43 -07001151 kfree(string.pointer);
Kristen Accardi783c49f2006-03-03 10:16:05 -08001152 return -1;
1153}
1154#endif
1155
1156
1157
rajesh.shah@intel.comed6cbcf2005-10-31 16:20:09 -08001158int pcie_init(struct controller * ctrl, struct pcie_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 u16 temp_word;
1162 u16 cap_reg;
1163 u16 intr_enable = 0;
1164 u32 slot_cap;
Kenji Kaneshige75e13172006-12-21 17:01:08 -08001165 int cap_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 u16 slot_status, slot_ctrl;
1167 struct pci_dev *pdev;
1168
1169 DBG_ENTER_ROUTINE
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 pdev = dev->port;
Kenji Kaneshige48fe3912006-12-21 17:01:04 -08001172 ctrl->pci_dev = pdev; /* save pci_dev in context */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
rajesh.shah@intel.com1a9ed1b2005-10-31 16:20:10 -08001174 dbg("%s: hotplug controller vendor id 0x%x device id 0x%x\n",
1175 __FUNCTION__, pdev->vendor, pdev->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 if ((cap_base = pci_find_capability(pdev, PCI_CAP_ID_EXP)) == 0) {
1178 dbg("%s: Can't find PCI_CAP_ID_EXP (0x10)\n", __FUNCTION__);
1179 goto abort_free_ctlr;
1180 }
1181
Dely Sy8b245e42005-05-06 17:19:09 -07001182 ctrl->cap_base = cap_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Kenji Kaneshige75e13172006-12-21 17:01:08 -08001184 dbg("%s: pcie_cap_base %x\n", __FUNCTION__, cap_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001186 rc = pciehp_readw(ctrl, CAPREG, &cap_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001188 err("%s: Cannot read CAPREG register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 goto abort_free_ctlr;
1190 }
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001191 dbg("%s: CAPREG offset %x cap_reg %x\n",
1192 __FUNCTION__, ctrl->cap_base + CAPREG, cap_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Dely Sy8b245e42005-05-06 17:19:09 -07001194 if (((cap_reg & SLOT_IMPL) == 0) || (((cap_reg & DEV_PORT_TYPE) != 0x0040)
1195 && ((cap_reg & DEV_PORT_TYPE) != 0x0060))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 dbg("%s : This is not a root port or the port is not connected to a slot\n", __FUNCTION__);
1197 goto abort_free_ctlr;
1198 }
1199
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001200 rc = pciehp_readl(ctrl, SLOTCAP, &slot_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001202 err("%s: Cannot read SLOTCAP register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 goto abort_free_ctlr;
1204 }
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001205 dbg("%s: SLOTCAP offset %x slot_cap %x\n",
1206 __FUNCTION__, ctrl->cap_base + SLOTCAP, slot_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 if (!(slot_cap & HP_CAP)) {
1209 dbg("%s : This slot is not hot-plug capable\n", __FUNCTION__);
1210 goto abort_free_ctlr;
1211 }
1212 /* For debugging purpose */
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001213 rc = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001215 err("%s: Cannot read SLOTSTATUS register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 goto abort_free_ctlr;
1217 }
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001218 dbg("%s: SLOTSTATUS offset %x slot_status %x\n",
1219 __FUNCTION__, ctrl->cap_base + SLOTSTATUS, slot_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001221 rc = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001223 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 goto abort_free_ctlr;
1225 }
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001226 dbg("%s: SLOTCTRL offset %x slot_ctrl %x\n",
1227 __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 for ( rc = 0; rc < DEVICE_COUNT_RESOURCE; rc++)
1230 if (pci_resource_len(pdev, rc) > 0)
Greg Kroah-Hartman1396a8c2006-06-12 15:14:29 -07001231 dbg("pci resource[%d] start=0x%llx(len=0x%llx)\n", rc,
1232 (unsigned long long)pci_resource_start(pdev, rc),
1233 (unsigned long long)pci_resource_len(pdev, rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 info("HPC vendor_id %x device_id %x ss_vid %x ss_did %x\n", pdev->vendor, pdev->device,
1236 pdev->subsystem_vendor, pdev->subsystem_device);
1237
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001238 mutex_init(&ctrl->crit_sect);
Kenji Kaneshigedd5619c2006-09-22 10:17:29 -07001239 mutex_init(&ctrl->ctrl_lock);
1240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 /* setup wait queue */
1242 init_waitqueue_head(&ctrl->queue);
1243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 /* return PCI Controller Info */
Kenji Kaneshige48fe3912006-12-21 17:01:04 -08001245 ctrl->slot_device_offset = 0;
1246 ctrl->num_slots = 1;
1247 ctrl->first_slot = slot_cap >> 19;
1248 ctrl->ctrlcap = slot_cap & 0x0000007f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 /* Mask Hot-plug Interrupt Enable */
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001251 rc = pciehp_readw(ctrl, SLOTCTRL, &temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001253 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 goto abort_free_ctlr;
1255 }
1256
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001257 dbg("%s: SLOTCTRL %x value read %x\n",
1258 __FUNCTION__, ctrl->cap_base + SLOTCTRL, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 temp_word = (temp_word & ~HP_INTR_ENABLE & ~CMD_CMPL_INTR_ENABLE) | 0x00;
1260
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001261 rc = pciehp_writew(ctrl, SLOTCTRL, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001263 err("%s: Cannot write to SLOTCTRL register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 goto abort_free_ctlr;
1265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001267 rc = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001269 err("%s: Cannot read SLOTSTATUS register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 goto abort_free_ctlr;
1271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 temp_word = 0x1F; /* Clear all events */
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001274 rc = pciehp_writew(ctrl, SLOTSTATUS, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001276 err("%s: Cannot write to SLOTSTATUS register\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 goto abort_free_ctlr;
1278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Kenji Kaneshige48fe3912006-12-21 17:01:04 -08001280 if (pciehp_poll_mode) {
1281 /* Install interrupt polling timer. Start with 10 sec delay */
1282 init_timer(&ctrl->poll_timer);
1283 start_int_poll_timer(ctrl, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 } else {
1285 /* Installs the interrupt handler */
Kenji Kaneshige48fe3912006-12-21 17:01:04 -08001286 rc = request_irq(ctrl->pci_dev->irq, pcie_isr, IRQF_SHARED,
1287 MY_NAME, (void *)ctrl);
1288 dbg("%s: request_irq %d for hpc%d (returns %d)\n",
Kenji Kaneshige5d386e12007-03-06 15:02:26 -08001289 __FUNCTION__, ctrl->pci_dev->irq,
1290 atomic_read(&pciehp_num_controllers), rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 if (rc) {
Kenji Kaneshige48fe3912006-12-21 17:01:04 -08001292 err("Can't get irq %d for the hotplug controller\n",
1293 ctrl->pci_dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 goto abort_free_ctlr;
1295 }
1296 }
rajesh.shah@intel.com1a9ed1b2005-10-31 16:20:10 -08001297 dbg("pciehp ctrl b:d:f:irq=0x%x:%x:%x:%x\n", pdev->bus->number,
1298 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn), dev->irq);
1299
Kenji Kaneshige5d386e12007-03-06 15:02:26 -08001300 /*
1301 * If this is the first controller to be initialized,
1302 * initialize the pciehp work queue
1303 */
1304 if (atomic_add_return(1, &pciehp_num_controllers) == 1) {
1305 pciehp_wq = create_singlethread_workqueue("pciehpd");
1306 if (!pciehp_wq) {
1307 rc = -ENOMEM;
1308 goto abort_free_irq;
1309 }
1310 }
1311
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001312 rc = pciehp_readw(ctrl, SLOTCTRL, &temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001314 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
Jan Beulich9c64f972006-05-09 00:50:31 -07001315 goto abort_free_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318 intr_enable = intr_enable | PRSN_DETECT_ENABLE;
1319
1320 if (ATTN_BUTTN(slot_cap))
1321 intr_enable = intr_enable | ATTN_BUTTN_ENABLE;
1322
1323 if (POWER_CTRL(slot_cap))
1324 intr_enable = intr_enable | PWR_FAULT_DETECT_ENABLE;
1325
1326 if (MRL_SENS(slot_cap))
1327 intr_enable = intr_enable | MRL_DETECT_ENABLE;
1328
1329 temp_word = (temp_word & ~intr_enable) | intr_enable;
1330
1331 if (pciehp_poll_mode) {
1332 temp_word = (temp_word & ~HP_INTR_ENABLE) | 0x0;
1333 } else {
1334 temp_word = (temp_word & ~HP_INTR_ENABLE) | HP_INTR_ENABLE;
1335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 /* Unmask Hot-plug Interrupt Enable for the interrupt notification mechanism case */
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001338 rc = pciehp_writew(ctrl, SLOTCTRL, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001340 err("%s: Cannot write to SLOTCTRL register\n", __FUNCTION__);
Jan Beulich9c64f972006-05-09 00:50:31 -07001341 goto abort_free_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 }
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001343 rc = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001345 err("%s: Cannot read SLOTSTATUS register\n", __FUNCTION__);
Jan Beulich9c64f972006-05-09 00:50:31 -07001346 goto abort_disable_intr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 temp_word = 0x1F; /* Clear all events */
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001350 rc = pciehp_writew(ctrl, SLOTSTATUS, temp_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 if (rc) {
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001352 err("%s: Cannot write to SLOTSTATUS register\n", __FUNCTION__);
Jan Beulich9c64f972006-05-09 00:50:31 -07001353 goto abort_disable_intr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
rajesh.shah@intel.coma3a45ec2005-10-31 16:20:12 -08001356 if (pciehp_force) {
1357 dbg("Bypassing BIOS check for pciehp use on %s\n",
1358 pci_name(ctrl->pci_dev));
1359 } else {
Rajesh Shah6560aa52005-11-07 13:37:36 -08001360 rc = pciehp_get_hp_hw_control_from_firmware(ctrl->pci_dev);
rajesh.shah@intel.coma3a45ec2005-10-31 16:20:12 -08001361 if (rc)
Jan Beulich9c64f972006-05-09 00:50:31 -07001362 goto abort_disable_intr;
rajesh.shah@intel.coma3a45ec2005-10-31 16:20:12 -08001363 }
rajesh.shah@intel.coma8a2be92005-10-31 16:20:07 -08001364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 ctrl->hpc_ops = &pciehp_hpc_ops;
1366
1367 DBG_LEAVE_ROUTINE
1368 return 0;
1369
1370 /* We end up here for the many possible ways to fail this API. */
Jan Beulich9c64f972006-05-09 00:50:31 -07001371abort_disable_intr:
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001372 rc = pciehp_readw(ctrl, SLOTCTRL, &temp_word);
Jan Beulich9c64f972006-05-09 00:50:31 -07001373 if (!rc) {
1374 temp_word &= ~(intr_enable | HP_INTR_ENABLE);
Kenji Kaneshigea0f018d2006-12-21 17:01:06 -08001375 rc = pciehp_writew(ctrl, SLOTCTRL, temp_word);
Jan Beulich9c64f972006-05-09 00:50:31 -07001376 }
1377 if (rc)
1378 err("%s : disabling interrupts failed\n", __FUNCTION__);
1379
1380abort_free_irq:
1381 if (pciehp_poll_mode)
Kenji Kaneshige48fe3912006-12-21 17:01:04 -08001382 del_timer_sync(&ctrl->poll_timer);
Jan Beulich9c64f972006-05-09 00:50:31 -07001383 else
Kenji Kaneshige48fe3912006-12-21 17:01:04 -08001384 free_irq(ctrl->pci_dev->irq, ctrl);
Jan Beulich9c64f972006-05-09 00:50:31 -07001385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386abort_free_ctlr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 DBG_LEAVE_ROUTINE
1388 return -1;
1389}