]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/pci/hotplug/pciehp_hpc.c
pciehp: cleanup register access
[linux-2.6-omap-h63xx.git] / drivers / pci / hotplug / pciehp_hpc.c
1 /*
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  *
26  * Send feedback to <greg@kroah.com>,<kristen.c.accardi@intel.com>
27  *
28  */
29
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/types.h>
33 #include <linux/signal.h>
34 #include <linux/jiffies.h>
35 #include <linux/timer.h>
36 #include <linux/pci.h>
37 #include <linux/interrupt.h>
38
39 #include "../pci.h"
40 #include "pciehp.h"
41 #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
73 struct 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 */
92 enum 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 };
108 static int pcie_cap_base = 0;           /* Base of the PCI Express capability item structure */ 
109
110 static inline int pciehp_readw(struct controller *ctrl, int reg, u16 *value)
111 {
112         struct pci_dev *dev = ctrl->pci_dev;
113         return pci_read_config_word(dev, ctrl->cap_base + reg, value);
114 }
115
116 static inline int pciehp_readl(struct controller *ctrl, int reg, u32 *value)
117 {
118         struct pci_dev *dev = ctrl->pci_dev;
119         return pci_read_config_dword(dev, ctrl->cap_base + reg, value);
120 }
121
122 static inline int pciehp_writew(struct controller *ctrl, int reg, u16 value)
123 {
124         struct pci_dev *dev = ctrl->pci_dev;
125         return pci_write_config_word(dev, ctrl->cap_base + reg, value);
126 }
127
128 static inline int pciehp_writel(struct controller *ctrl, int reg, u32 value)
129 {
130         struct pci_dev *dev = ctrl->pci_dev;
131         return pci_write_config_dword(dev, ctrl->cap_base + reg, value);
132 }
133
134 /* Field definitions in PCI Express Capabilities Register */
135 #define CAP_VER                 0x000F
136 #define DEV_PORT_TYPE           0x00F0
137 #define SLOT_IMPL               0x0100
138 #define MSG_NUM                 0x3E00
139
140 /* Device or Port Type */
141 #define NAT_ENDPT               0x00
142 #define LEG_ENDPT               0x01
143 #define ROOT_PORT               0x04
144 #define UP_STREAM               0x05
145 #define DN_STREAM               0x06
146 #define PCIE_PCI_BRDG           0x07
147 #define PCI_PCIE_BRDG           0x10
148
149 /* Field definitions in Device Capabilities Register */
150 #define DATTN_BUTTN_PRSN        0x1000
151 #define DATTN_LED_PRSN          0x2000
152 #define DPWR_LED_PRSN           0x4000
153
154 /* Field definitions in Link Capabilities Register */
155 #define MAX_LNK_SPEED           0x000F
156 #define MAX_LNK_WIDTH           0x03F0
157
158 /* Link Width Encoding */
159 #define LNK_X1          0x01
160 #define LNK_X2          0x02
161 #define LNK_X4          0x04    
162 #define LNK_X8          0x08
163 #define LNK_X12         0x0C
164 #define LNK_X16         0x10    
165 #define LNK_X32         0x20
166
167 /*Field definitions of Link Status Register */
168 #define LNK_SPEED       0x000F
169 #define NEG_LINK_WD     0x03F0
170 #define LNK_TRN_ERR     0x0400
171 #define LNK_TRN         0x0800
172 #define SLOT_CLK_CONF   0x1000
173
174 /* Field definitions in Slot Capabilities Register */
175 #define ATTN_BUTTN_PRSN 0x00000001
176 #define PWR_CTRL_PRSN   0x00000002
177 #define MRL_SENS_PRSN   0x00000004
178 #define ATTN_LED_PRSN   0x00000008
179 #define PWR_LED_PRSN    0x00000010
180 #define HP_SUPR_RM_SUP  0x00000020
181 #define HP_CAP          0x00000040
182 #define SLOT_PWR_VALUE  0x000003F8
183 #define SLOT_PWR_LIMIT  0x00000C00
184 #define PSN             0xFFF80000      /* PSN: Physical Slot Number */
185
186 /* Field definitions in Slot Control Register */
187 #define ATTN_BUTTN_ENABLE               0x0001
188 #define PWR_FAULT_DETECT_ENABLE         0x0002
189 #define MRL_DETECT_ENABLE               0x0004
190 #define PRSN_DETECT_ENABLE              0x0008
191 #define CMD_CMPL_INTR_ENABLE            0x0010
192 #define HP_INTR_ENABLE                  0x0020
193 #define ATTN_LED_CTRL                   0x00C0
194 #define PWR_LED_CTRL                    0x0300
195 #define PWR_CTRL                        0x0400
196
197 /* Attention indicator and Power indicator states */
198 #define LED_ON          0x01
199 #define LED_BLINK       0x10
200 #define LED_OFF         0x11
201
202 /* Power Control Command */
203 #define POWER_ON        0
204 #define POWER_OFF       0x0400
205
206 /* Field definitions in Slot Status Register */
207 #define ATTN_BUTTN_PRESSED      0x0001
208 #define PWR_FAULT_DETECTED      0x0002
209 #define MRL_SENS_CHANGED        0x0004
210 #define PRSN_DETECT_CHANGED     0x0008
211 #define CMD_COMPLETED           0x0010
212 #define MRL_STATE               0x0020
213 #define PRSN_STATE              0x0040
214
215 static spinlock_t hpc_event_lock;
216
217 DEFINE_DBG_BUFFER               /* Debug string buffer for entire HPC defined here */
218 static int ctlr_seq_num = 0;    /* Controller sequence # */
219
220 static irqreturn_t pcie_isr(int irq, void *dev_id);
221 static void start_int_poll_timer(struct controller *ctrl, int sec);
222
223 /* This is the interrupt polling timeout function. */
224 static void int_poll_timeout(unsigned long data)
225 {
226         struct controller *ctrl = (struct controller *)data;
227
228         DBG_ENTER_ROUTINE
229
230         /* Poll for interrupt events.  regs == NULL => polling */
231         pcie_isr(0, ctrl);
232
233         init_timer(&ctrl->poll_timer);
234         if (!pciehp_poll_time)
235                 pciehp_poll_time = 2; /* reset timer to poll in 2 secs if user doesn't specify at module installation*/
236
237         start_int_poll_timer(ctrl, pciehp_poll_time);
238 }
239
240 /* This function starts the interrupt polling timer. */
241 static void start_int_poll_timer(struct controller *ctrl, int sec)
242 {
243         /* Clamp to sane value */
244         if ((sec <= 0) || (sec > 60))
245                 sec = 2;
246
247         ctrl->poll_timer.function = &int_poll_timeout;
248         ctrl->poll_timer.data = (unsigned long)ctrl;
249         ctrl->poll_timer.expires = jiffies + sec * HZ;
250         add_timer(&ctrl->poll_timer);
251 }
252
253 static int pcie_write_cmd(struct slot *slot, u16 cmd)
254 {
255         struct controller *ctrl = slot->ctrl;
256         int retval = 0;
257         u16 slot_status;
258
259         DBG_ENTER_ROUTINE 
260
261         retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
262         if (retval) {
263                 err("%s: Cannot read SLOTSTATUS register\n", __FUNCTION__);
264                 return retval;
265         }
266
267         if ((slot_status & CMD_COMPLETED) == CMD_COMPLETED ) { 
268                 /* After 1 sec and CMD_COMPLETED still not set, just proceed forward to issue 
269                    the next command according to spec.  Just print out the error message */
270                 dbg("%s : CMD_COMPLETED not clear after 1 sec.\n", __FUNCTION__);
271         }
272
273         retval = pciehp_writew(ctrl, SLOTCTRL, (cmd | CMD_CMPL_INTR_ENABLE));
274         if (retval) {
275                 err("%s: Cannot write to SLOTCTRL register\n", __FUNCTION__);
276                 return retval;
277         }
278
279         DBG_LEAVE_ROUTINE 
280         return retval;
281 }
282
283 static int hpc_check_lnk_status(struct controller *ctrl)
284 {
285         u16 lnk_status;
286         int retval = 0;
287
288         DBG_ENTER_ROUTINE 
289
290         retval = pciehp_readw(ctrl, LNKSTATUS, &lnk_status);
291         if (retval) {
292                 err("%s: Cannot read LNKSTATUS register\n", __FUNCTION__);
293                 return retval;
294         }
295
296         dbg("%s: lnk_status = %x\n", __FUNCTION__, lnk_status);
297         if ( (lnk_status & LNK_TRN) || (lnk_status & LNK_TRN_ERR) || 
298                 !(lnk_status & NEG_LINK_WD)) {
299                 err("%s : Link Training Error occurs \n", __FUNCTION__);
300                 retval = -1;
301                 return retval;
302         }
303
304         DBG_LEAVE_ROUTINE 
305         return retval;
306 }
307
308
309 static int hpc_get_attention_status(struct slot *slot, u8 *status)
310 {
311         struct controller *ctrl = slot->ctrl;
312         u16 slot_ctrl;
313         u8 atten_led_state;
314         int retval = 0;
315         
316         DBG_ENTER_ROUTINE 
317
318         retval = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
319         if (retval) {
320                 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
321                 return retval;
322         }
323
324         dbg("%s: SLOTCTRL %x, value read %x\n",
325             __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_ctrl);
326
327         atten_led_state = (slot_ctrl & ATTN_LED_CTRL) >> 6;
328
329         switch (atten_led_state) {
330         case 0:
331                 *status = 0xFF; /* Reserved */
332                 break;
333         case 1:
334                 *status = 1;    /* On */
335                 break;
336         case 2:
337                 *status = 2;    /* Blink */
338                 break;
339         case 3:
340                 *status = 0;    /* Off */
341                 break;
342         default:
343                 *status = 0xFF;
344                 break;
345         }
346
347         DBG_LEAVE_ROUTINE 
348         return 0;
349 }
350
351 static int hpc_get_power_status(struct slot *slot, u8 *status)
352 {
353         struct controller *ctrl = slot->ctrl;
354         u16 slot_ctrl;
355         u8 pwr_state;
356         int     retval = 0;
357         
358         DBG_ENTER_ROUTINE 
359
360         retval = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
361         if (retval) {
362                 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
363                 return retval;
364         }
365         dbg("%s: SLOTCTRL %x value read %x\n",
366             __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_ctrl);
367
368         pwr_state = (slot_ctrl & PWR_CTRL) >> 10;
369
370         switch (pwr_state) {
371         case 0:
372                 *status = 1;
373                 break;
374         case 1:
375                 *status = 0;    
376                 break;
377         default:
378                 *status = 0xFF;
379                 break;
380         }
381
382         DBG_LEAVE_ROUTINE 
383         return retval;
384 }
385
386
387 static int hpc_get_latch_status(struct slot *slot, u8 *status)
388 {
389         struct controller *ctrl = slot->ctrl;
390         u16 slot_status;
391         int retval = 0;
392
393         DBG_ENTER_ROUTINE 
394
395         retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
396         if (retval) {
397                 err("%s: Cannot read SLOTSTATUS register\n", __FUNCTION__);
398                 return retval;
399         }
400
401         *status = (((slot_status & MRL_STATE) >> 5) == 0) ? 0 : 1;  
402
403         DBG_LEAVE_ROUTINE 
404         return 0;
405 }
406
407 static int hpc_get_adapter_status(struct slot *slot, u8 *status)
408 {
409         struct controller *ctrl = slot->ctrl;
410         u16 slot_status;
411         u8 card_state;
412         int retval = 0;
413
414         DBG_ENTER_ROUTINE 
415
416         retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
417         if (retval) {
418                 err("%s: Cannot read SLOTSTATUS register\n", __FUNCTION__);
419                 return retval;
420         }
421         card_state = (u8)((slot_status & PRSN_STATE) >> 6);
422         *status = (card_state == 1) ? 1 : 0;
423
424         DBG_LEAVE_ROUTINE 
425         return 0;
426 }
427
428 static int hpc_query_power_fault(struct slot *slot)
429 {
430         struct controller *ctrl = slot->ctrl;
431         u16 slot_status;
432         u8 pwr_fault;
433         int retval = 0;
434
435         DBG_ENTER_ROUTINE 
436
437         retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
438         if (retval) {
439                 err("%s: Cannot check for power fault\n", __FUNCTION__);
440                 return retval;
441         }
442         pwr_fault = (u8)((slot_status & PWR_FAULT_DETECTED) >> 1);
443         
444         DBG_LEAVE_ROUTINE
445         return pwr_fault;
446 }
447
448 static int hpc_set_attention_status(struct slot *slot, u8 value)
449 {
450         struct controller *ctrl = slot->ctrl;
451         u16 slot_cmd = 0;
452         u16 slot_ctrl;
453         int rc = 0;
454
455         DBG_ENTER_ROUTINE
456
457         rc = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
458         if (rc) {
459                 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
460                 return rc;
461         }
462
463         switch (value) {
464                 case 0 :        /* turn off */
465                         slot_cmd = (slot_ctrl & ~ATTN_LED_CTRL) | 0x00C0;
466                         break;
467                 case 1:         /* turn on */
468                         slot_cmd = (slot_ctrl & ~ATTN_LED_CTRL) | 0x0040;
469                         break;
470                 case 2:         /* turn blink */
471                         slot_cmd = (slot_ctrl & ~ATTN_LED_CTRL) | 0x0080;
472                         break;
473                 default:
474                         return -1;
475         }
476         if (!pciehp_poll_mode)
477                 slot_cmd = slot_cmd | HP_INTR_ENABLE; 
478
479         pcie_write_cmd(slot, slot_cmd);
480         dbg("%s: SLOTCTRL %x write cmd %x\n",
481             __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_cmd);
482         
483         DBG_LEAVE_ROUTINE
484         return rc;
485 }
486
487
488 static void hpc_set_green_led_on(struct slot *slot)
489 {
490         struct controller *ctrl = slot->ctrl;
491         u16 slot_cmd;
492         u16 slot_ctrl;
493         int rc = 0;
494         
495         DBG_ENTER_ROUTINE
496
497         rc = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
498         if (rc) {
499                 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
500                 return;
501         }
502         slot_cmd = (slot_ctrl & ~PWR_LED_CTRL) | 0x0100;
503         if (!pciehp_poll_mode)
504                 slot_cmd = slot_cmd | HP_INTR_ENABLE; 
505
506         pcie_write_cmd(slot, slot_cmd);
507
508         dbg("%s: SLOTCTRL %x write cmd %x\n",
509             __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_cmd);
510         DBG_LEAVE_ROUTINE
511         return;
512 }
513
514 static void hpc_set_green_led_off(struct slot *slot)
515 {
516         struct controller *ctrl = slot->ctrl;
517         u16 slot_cmd;
518         u16 slot_ctrl;
519         int rc = 0;
520
521         DBG_ENTER_ROUTINE
522
523         rc = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
524         if (rc) {
525                 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
526                 return;
527         }
528
529         slot_cmd = (slot_ctrl & ~PWR_LED_CTRL) | 0x0300;
530
531         if (!pciehp_poll_mode)
532                 slot_cmd = slot_cmd | HP_INTR_ENABLE; 
533         pcie_write_cmd(slot, slot_cmd);
534         dbg("%s: SLOTCTRL %x write cmd %x\n",
535             __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_cmd);
536
537         DBG_LEAVE_ROUTINE
538         return;
539 }
540
541 static void hpc_set_green_led_blink(struct slot *slot)
542 {
543         struct controller *ctrl = slot->ctrl;
544         u16 slot_cmd;
545         u16 slot_ctrl;
546         int rc = 0; 
547         
548         DBG_ENTER_ROUTINE
549
550         rc = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
551         if (rc) {
552                 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
553                 return;
554         }
555
556         slot_cmd = (slot_ctrl & ~PWR_LED_CTRL) | 0x0200;
557
558         if (!pciehp_poll_mode)
559                 slot_cmd = slot_cmd | HP_INTR_ENABLE; 
560         pcie_write_cmd(slot, slot_cmd);
561
562         dbg("%s: SLOTCTRL %x write cmd %x\n",
563             __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_cmd);
564         DBG_LEAVE_ROUTINE
565         return;
566 }
567
568 static void hpc_release_ctlr(struct controller *ctrl)
569 {
570         DBG_ENTER_ROUTINE 
571
572         if (pciehp_poll_mode)
573                 del_timer(&ctrl->poll_timer);
574         else
575                 free_irq(ctrl->pci_dev->irq, ctrl);
576
577         DBG_LEAVE_ROUTINE
578 }
579
580 static int hpc_power_on_slot(struct slot * slot)
581 {
582         struct controller *ctrl = slot->ctrl;
583         u16 slot_cmd;
584         u16 slot_ctrl, slot_status;
585         int retval = 0;
586
587         DBG_ENTER_ROUTINE 
588
589         dbg("%s: slot->hp_slot %x\n", __FUNCTION__, slot->hp_slot);
590
591         /* Clear sticky power-fault bit from previous power failures */
592         retval = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
593         if (retval) {
594                 err("%s: Cannot read SLOTSTATUS register\n", __FUNCTION__);
595                 return retval;
596         }
597         slot_status &= PWR_FAULT_DETECTED;
598         if (slot_status) {
599                 retval = pciehp_writew(ctrl, SLOTSTATUS, slot_status);
600                 if (retval) {
601                         err("%s: Cannot write to SLOTSTATUS register\n",
602                             __FUNCTION__);
603                         return retval;
604                 }
605         }
606
607         retval = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
608         if (retval) {
609                 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
610                 return retval;
611         }
612
613         slot_cmd = (slot_ctrl & ~PWR_CTRL) | POWER_ON;
614
615         /* Enable detection that we turned off at slot power-off time */
616         if (!pciehp_poll_mode)
617                 slot_cmd = slot_cmd |
618                            PWR_FAULT_DETECT_ENABLE |
619                            MRL_DETECT_ENABLE |
620                            PRSN_DETECT_ENABLE |
621                            HP_INTR_ENABLE;
622
623         retval = pcie_write_cmd(slot, slot_cmd);
624
625         if (retval) {
626                 err("%s: Write %x command failed!\n", __FUNCTION__, slot_cmd);
627                 return -1;
628         }
629         dbg("%s: SLOTCTRL %x write cmd %x\n",
630             __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_cmd);
631
632         DBG_LEAVE_ROUTINE
633
634         return retval;
635 }
636
637 static int hpc_power_off_slot(struct slot * slot)
638 {
639         struct controller *ctrl = slot->ctrl;
640         u16 slot_cmd;
641         u16 slot_ctrl;
642         int retval = 0;
643
644         DBG_ENTER_ROUTINE 
645
646         dbg("%s: slot->hp_slot %x\n", __FUNCTION__, slot->hp_slot);
647
648         retval = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
649         if (retval) {
650                 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
651                 return retval;
652         }
653
654         slot_cmd = (slot_ctrl & ~PWR_CTRL) | POWER_OFF;
655
656         /*
657          * If we get MRL or presence detect interrupts now, the isr
658          * will notice the sticky power-fault bit too and issue power
659          * indicator change commands. This will lead to an endless loop
660          * of command completions, since the power-fault bit remains on
661          * till the slot is powered on again.
662          */
663         if (!pciehp_poll_mode)
664                 slot_cmd = (slot_cmd &
665                             ~PWR_FAULT_DETECT_ENABLE &
666                             ~MRL_DETECT_ENABLE &
667                             ~PRSN_DETECT_ENABLE) | HP_INTR_ENABLE;
668
669         retval = pcie_write_cmd(slot, slot_cmd);
670
671         if (retval) {
672                 err("%s: Write command failed!\n", __FUNCTION__);
673                 return -1;
674         }
675         dbg("%s: SLOTCTRL %x write cmd %x\n",
676             __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_cmd);
677
678         DBG_LEAVE_ROUTINE
679
680         return retval;
681 }
682
683 static irqreturn_t pcie_isr(int irq, void *dev_id)
684 {
685         struct controller *ctrl = (struct controller *)dev_id;
686         u16 slot_status, intr_detect, intr_loc;
687         u16 temp_word;
688         int hp_slot = 0;        /* only 1 slot per PCI Express port */
689         int rc = 0;
690
691         rc = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
692         if (rc) {
693                 err("%s: Cannot read SLOTSTATUS register\n", __FUNCTION__);
694                 return IRQ_NONE;
695         }
696
697         intr_detect = ( ATTN_BUTTN_PRESSED | PWR_FAULT_DETECTED | MRL_SENS_CHANGED |
698                                         PRSN_DETECT_CHANGED | CMD_COMPLETED );
699
700         intr_loc = slot_status & intr_detect;
701
702         /* Check to see if it was our interrupt */
703         if ( !intr_loc )
704                 return IRQ_NONE;
705
706         dbg("%s: intr_loc %x\n", __FUNCTION__, intr_loc);
707         /* Mask Hot-plug Interrupt Enable */
708         if (!pciehp_poll_mode) {
709                 rc = pciehp_readw(ctrl, SLOTCTRL, &temp_word);
710                 if (rc) {
711                         err("%s: Cannot read SLOT_CTRL register\n",
712                             __FUNCTION__);
713                         return IRQ_NONE;
714                 }
715
716                 dbg("%s: pciehp_readw(SLOTCTRL) with value %x\n",
717                     __FUNCTION__, temp_word);
718                 temp_word = (temp_word & ~HP_INTR_ENABLE & ~CMD_CMPL_INTR_ENABLE) | 0x00;
719                 rc = pciehp_writew(ctrl, SLOTCTRL, temp_word);
720                 if (rc) {
721                         err("%s: Cannot write to SLOTCTRL register\n",
722                             __FUNCTION__);
723                         return IRQ_NONE;
724                 }
725
726                 rc = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
727                 if (rc) {
728                         err("%s: Cannot read SLOT_STATUS register\n",
729                             __FUNCTION__);
730                         return IRQ_NONE;
731                 }
732                 dbg("%s: pciehp_readw(SLOTSTATUS) with value %x\n",
733                     __FUNCTION__, slot_status);
734                 
735                 /* Clear command complete interrupt caused by this write */
736                 temp_word = 0x1f;
737                 rc = pciehp_writew(ctrl, SLOTSTATUS, temp_word);
738                 if (rc) {
739                         err("%s: Cannot write to SLOTSTATUS register\n",
740                             __FUNCTION__);
741                         return IRQ_NONE;
742                 }
743         }
744         
745         if (intr_loc & CMD_COMPLETED) {
746                 /* 
747                  * Command Complete Interrupt Pending 
748                  */
749                 wake_up_interruptible(&ctrl->queue);
750         }
751
752         if (intr_loc & MRL_SENS_CHANGED)
753                 pciehp_handle_switch_change(hp_slot, ctrl);
754
755         if (intr_loc & ATTN_BUTTN_PRESSED)
756                 pciehp_handle_attention_button(hp_slot, ctrl);
757
758         if (intr_loc & PRSN_DETECT_CHANGED)
759                 pciehp_handle_presence_change(hp_slot, ctrl);
760
761         if (intr_loc & PWR_FAULT_DETECTED)
762                 pciehp_handle_power_fault(hp_slot, ctrl);
763
764         /* Clear all events after serving them */
765         temp_word = 0x1F;
766         rc = pciehp_writew(ctrl, SLOTSTATUS, temp_word);
767         if (rc) {
768                 err("%s: Cannot write to SLOTSTATUS register\n", __FUNCTION__);
769                 return IRQ_NONE;
770         }
771         /* Unmask Hot-plug Interrupt Enable */
772         if (!pciehp_poll_mode) {
773                 rc = pciehp_readw(ctrl, SLOTCTRL, &temp_word);
774                 if (rc) {
775                         err("%s: Cannot read SLOTCTRL register\n",
776                             __FUNCTION__);
777                         return IRQ_NONE;
778                 }
779
780                 dbg("%s: Unmask Hot-plug Interrupt Enable\n", __FUNCTION__);
781                 temp_word = (temp_word & ~HP_INTR_ENABLE) | HP_INTR_ENABLE;
782
783                 rc = pciehp_writew(ctrl, SLOTCTRL, temp_word);
784                 if (rc) {
785                         err("%s: Cannot write to SLOTCTRL register\n",
786                             __FUNCTION__);
787                         return IRQ_NONE;
788                 }
789
790                 rc = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
791                 if (rc) {
792                         err("%s: Cannot read SLOT_STATUS register\n",
793                             __FUNCTION__);
794                         return IRQ_NONE;
795                 }
796                 
797                 /* Clear command complete interrupt caused by this write */
798                 temp_word = 0x1F;
799                 rc = pciehp_writew(ctrl, SLOTSTATUS, temp_word);
800                 if (rc) {
801                         err("%s: Cannot write to SLOTSTATUS failed\n",
802                             __FUNCTION__);
803                         return IRQ_NONE;
804                 }
805                 dbg("%s: pciehp_writew(SLOTSTATUS) with value %x\n",
806                     __FUNCTION__, temp_word);
807         }
808         
809         return IRQ_HANDLED;
810 }
811
812 static int hpc_get_max_lnk_speed (struct slot *slot, enum pci_bus_speed *value)
813 {
814         struct controller *ctrl = slot->ctrl;
815         enum pcie_link_speed lnk_speed;
816         u32     lnk_cap;
817         int retval = 0;
818
819         DBG_ENTER_ROUTINE 
820
821         retval = pciehp_readl(ctrl, LNKCAP, &lnk_cap);
822         if (retval) {
823                 err("%s: Cannot read LNKCAP register\n", __FUNCTION__);
824                 return retval;
825         }
826
827         switch (lnk_cap & 0x000F) {
828         case 1:
829                 lnk_speed = PCIE_2PT5GB;
830                 break;
831         default:
832                 lnk_speed = PCIE_LNK_SPEED_UNKNOWN;
833                 break;
834         }
835
836         *value = lnk_speed;
837         dbg("Max link speed = %d\n", lnk_speed);
838         DBG_LEAVE_ROUTINE 
839         return retval;
840 }
841
842 static int hpc_get_max_lnk_width (struct slot *slot, enum pcie_link_width *value)
843 {
844         struct controller *ctrl = slot->ctrl;
845         enum pcie_link_width lnk_wdth;
846         u32     lnk_cap;
847         int retval = 0;
848
849         DBG_ENTER_ROUTINE 
850
851         retval = pciehp_readl(ctrl, LNKCAP, &lnk_cap);
852         if (retval) {
853                 err("%s: Cannot read LNKCAP register\n", __FUNCTION__);
854                 return retval;
855         }
856
857         switch ((lnk_cap & 0x03F0) >> 4){
858         case 0:
859                 lnk_wdth = PCIE_LNK_WIDTH_RESRV;
860                 break;
861         case 1:
862                 lnk_wdth = PCIE_LNK_X1;
863                 break;
864         case 2:
865                 lnk_wdth = PCIE_LNK_X2;
866                 break;
867         case 4:
868                 lnk_wdth = PCIE_LNK_X4;
869                 break;
870         case 8:
871                 lnk_wdth = PCIE_LNK_X8;
872                 break;
873         case 12:
874                 lnk_wdth = PCIE_LNK_X12;
875                 break;
876         case 16:
877                 lnk_wdth = PCIE_LNK_X16;
878                 break;
879         case 32:
880                 lnk_wdth = PCIE_LNK_X32;
881                 break;
882         default:
883                 lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
884                 break;
885         }
886
887         *value = lnk_wdth;
888         dbg("Max link width = %d\n", lnk_wdth);
889         DBG_LEAVE_ROUTINE 
890         return retval;
891 }
892
893 static int hpc_get_cur_lnk_speed (struct slot *slot, enum pci_bus_speed *value)
894 {
895         struct controller *ctrl = slot->ctrl;
896         enum pcie_link_speed lnk_speed = PCI_SPEED_UNKNOWN;
897         int retval = 0;
898         u16 lnk_status;
899
900         DBG_ENTER_ROUTINE 
901
902         retval = pciehp_readw(ctrl, LNKSTATUS, &lnk_status);
903         if (retval) {
904                 err("%s: Cannot read LNKSTATUS register\n", __FUNCTION__);
905                 return retval;
906         }
907
908         switch (lnk_status & 0x0F) {
909         case 1:
910                 lnk_speed = PCIE_2PT5GB;
911                 break;
912         default:
913                 lnk_speed = PCIE_LNK_SPEED_UNKNOWN;
914                 break;
915         }
916
917         *value = lnk_speed;
918         dbg("Current link speed = %d\n", lnk_speed);
919         DBG_LEAVE_ROUTINE 
920         return retval;
921 }
922
923 static int hpc_get_cur_lnk_width (struct slot *slot, enum pcie_link_width *value)
924 {
925         struct controller *ctrl = slot->ctrl;
926         enum pcie_link_width lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
927         int retval = 0;
928         u16 lnk_status;
929
930         DBG_ENTER_ROUTINE 
931
932         retval = pciehp_readw(ctrl, LNKSTATUS, &lnk_status);
933         if (retval) {
934                 err("%s: Cannot read LNKSTATUS register\n", __FUNCTION__);
935                 return retval;
936         }
937         
938         switch ((lnk_status & 0x03F0) >> 4){
939         case 0:
940                 lnk_wdth = PCIE_LNK_WIDTH_RESRV;
941                 break;
942         case 1:
943                 lnk_wdth = PCIE_LNK_X1;
944                 break;
945         case 2:
946                 lnk_wdth = PCIE_LNK_X2;
947                 break;
948         case 4:
949                 lnk_wdth = PCIE_LNK_X4;
950                 break;
951         case 8:
952                 lnk_wdth = PCIE_LNK_X8;
953                 break;
954         case 12:
955                 lnk_wdth = PCIE_LNK_X12;
956                 break;
957         case 16:
958                 lnk_wdth = PCIE_LNK_X16;
959                 break;
960         case 32:
961                 lnk_wdth = PCIE_LNK_X32;
962                 break;
963         default:
964                 lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
965                 break;
966         }
967
968         *value = lnk_wdth;
969         dbg("Current link width = %d\n", lnk_wdth);
970         DBG_LEAVE_ROUTINE 
971         return retval;
972 }
973
974 static struct hpc_ops pciehp_hpc_ops = {
975         .power_on_slot                  = hpc_power_on_slot,
976         .power_off_slot                 = hpc_power_off_slot,
977         .set_attention_status           = hpc_set_attention_status,
978         .get_power_status               = hpc_get_power_status,
979         .get_attention_status           = hpc_get_attention_status,
980         .get_latch_status               = hpc_get_latch_status,
981         .get_adapter_status             = hpc_get_adapter_status,
982
983         .get_max_bus_speed              = hpc_get_max_lnk_speed,
984         .get_cur_bus_speed              = hpc_get_cur_lnk_speed,
985         .get_max_lnk_width              = hpc_get_max_lnk_width,
986         .get_cur_lnk_width              = hpc_get_cur_lnk_width,
987         
988         .query_power_fault              = hpc_query_power_fault,
989         .green_led_on                   = hpc_set_green_led_on,
990         .green_led_off                  = hpc_set_green_led_off,
991         .green_led_blink                = hpc_set_green_led_blink,
992         
993         .release_ctlr                   = hpc_release_ctlr,
994         .check_lnk_status               = hpc_check_lnk_status,
995 };
996
997 #ifdef CONFIG_ACPI
998 int pciehp_acpi_get_hp_hw_control_from_firmware(struct pci_dev *dev)
999 {
1000         acpi_status status;
1001         acpi_handle chandle, handle = DEVICE_ACPI_HANDLE(&(dev->dev));
1002         struct pci_dev *pdev = dev;
1003         struct pci_bus *parent;
1004         struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL };
1005
1006         /*
1007          * Per PCI firmware specification, we should run the ACPI _OSC
1008          * method to get control of hotplug hardware before using it.
1009          * If an _OSC is missing, we look for an OSHP to do the same thing.
1010          * To handle different BIOS behavior, we look for _OSC and OSHP
1011          * within the scope of the hotplug controller and its parents, upto
1012          * the host bridge under which this controller exists.
1013          */
1014         while (!handle) {
1015                 /*
1016                  * This hotplug controller was not listed in the ACPI name
1017                  * space at all. Try to get acpi handle of parent pci bus.
1018                  */
1019                 if (!pdev || !pdev->bus->parent)
1020                         break;
1021                 parent = pdev->bus->parent;
1022                 dbg("Could not find %s in acpi namespace, trying parent\n",
1023                                 pci_name(pdev));
1024                 if (!parent->self)
1025                         /* Parent must be a host bridge */
1026                         handle = acpi_get_pci_rootbridge_handle(
1027                                         pci_domain_nr(parent),
1028                                         parent->number);
1029                 else
1030                         handle = DEVICE_ACPI_HANDLE(
1031                                         &(parent->self->dev));
1032                 pdev = parent->self;
1033         }
1034
1035         while (handle) {
1036                 acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
1037                 dbg("Trying to get hotplug control for %s \n",
1038                         (char *)string.pointer);
1039                 status = pci_osc_control_set(handle,
1040                                 OSC_PCI_EXPRESS_NATIVE_HP_CONTROL);
1041                 if (status == AE_NOT_FOUND)
1042                         status = acpi_run_oshp(handle);
1043                 if (ACPI_SUCCESS(status)) {
1044                         dbg("Gained control for hotplug HW for pci %s (%s)\n",
1045                                 pci_name(dev), (char *)string.pointer);
1046                         kfree(string.pointer);
1047                         return 0;
1048                 }
1049                 if (acpi_root_bridge(handle))
1050                         break;
1051                 chandle = handle;
1052                 status = acpi_get_parent(chandle, &handle);
1053                 if (ACPI_FAILURE(status))
1054                         break;
1055         }
1056
1057         err("Cannot get control of hotplug hardware for pci %s\n",
1058                         pci_name(dev));
1059
1060         kfree(string.pointer);
1061         return -1;
1062 }
1063 #endif
1064
1065
1066
1067 int pcie_init(struct controller * ctrl, struct pcie_device *dev)
1068 {
1069         int rc;
1070         static int first = 1;
1071         u16 temp_word;
1072         u16 cap_reg;
1073         u16 intr_enable = 0;
1074         u32 slot_cap;
1075         int cap_base, saved_cap_base;
1076         u16 slot_status, slot_ctrl;
1077         struct pci_dev *pdev;
1078
1079         DBG_ENTER_ROUTINE
1080         
1081         pdev = dev->port;
1082         ctrl->pci_dev = pdev;   /* save pci_dev in context */
1083
1084         dbg("%s: hotplug controller vendor id 0x%x device id 0x%x\n",
1085                         __FUNCTION__, pdev->vendor, pdev->device);
1086
1087         saved_cap_base = pcie_cap_base;
1088
1089         if ((cap_base = pci_find_capability(pdev, PCI_CAP_ID_EXP)) == 0) {
1090                 dbg("%s: Can't find PCI_CAP_ID_EXP (0x10)\n", __FUNCTION__);
1091                 goto abort_free_ctlr;
1092         }
1093
1094         ctrl->cap_base = cap_base;
1095
1096         dbg("%s: pcie_cap_base %x\n", __FUNCTION__, pcie_cap_base);
1097
1098         rc = pciehp_readw(ctrl, CAPREG, &cap_reg);
1099         if (rc) {
1100                 err("%s: Cannot read CAPREG register\n", __FUNCTION__);
1101                 goto abort_free_ctlr;
1102         }
1103         dbg("%s: CAPREG offset %x cap_reg %x\n",
1104             __FUNCTION__, ctrl->cap_base + CAPREG, cap_reg);
1105
1106         if (((cap_reg & SLOT_IMPL) == 0) || (((cap_reg & DEV_PORT_TYPE) != 0x0040)
1107                 && ((cap_reg & DEV_PORT_TYPE) != 0x0060))) {
1108                 dbg("%s : This is not a root port or the port is not connected to a slot\n", __FUNCTION__);
1109                 goto abort_free_ctlr;
1110         }
1111
1112         rc = pciehp_readl(ctrl, SLOTCAP, &slot_cap);
1113         if (rc) {
1114                 err("%s: Cannot read SLOTCAP register\n", __FUNCTION__);
1115                 goto abort_free_ctlr;
1116         }
1117         dbg("%s: SLOTCAP offset %x slot_cap %x\n",
1118             __FUNCTION__, ctrl->cap_base + SLOTCAP, slot_cap);
1119
1120         if (!(slot_cap & HP_CAP)) {
1121                 dbg("%s : This slot is not hot-plug capable\n", __FUNCTION__);
1122                 goto abort_free_ctlr;
1123         }
1124         /* For debugging purpose */
1125         rc = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
1126         if (rc) {
1127                 err("%s: Cannot read SLOTSTATUS register\n", __FUNCTION__);
1128                 goto abort_free_ctlr;
1129         }
1130         dbg("%s: SLOTSTATUS offset %x slot_status %x\n",
1131             __FUNCTION__, ctrl->cap_base + SLOTSTATUS, slot_status);
1132
1133         rc = pciehp_readw(ctrl, SLOTCTRL, &slot_ctrl);
1134         if (rc) {
1135                 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
1136                 goto abort_free_ctlr;
1137         }
1138         dbg("%s: SLOTCTRL offset %x slot_ctrl %x\n",
1139             __FUNCTION__, ctrl->cap_base + SLOTCTRL, slot_ctrl);
1140
1141         if (first) {
1142                 spin_lock_init(&hpc_event_lock);
1143                 first = 0;
1144         }
1145
1146         for ( rc = 0; rc < DEVICE_COUNT_RESOURCE; rc++)
1147                 if (pci_resource_len(pdev, rc) > 0)
1148                         dbg("pci resource[%d] start=0x%llx(len=0x%llx)\n", rc,
1149                             (unsigned long long)pci_resource_start(pdev, rc),
1150                             (unsigned long long)pci_resource_len(pdev, rc));
1151
1152         info("HPC vendor_id %x device_id %x ss_vid %x ss_did %x\n", pdev->vendor, pdev->device, 
1153                 pdev->subsystem_vendor, pdev->subsystem_device);
1154
1155         mutex_init(&ctrl->crit_sect);
1156         mutex_init(&ctrl->ctrl_lock);
1157
1158         /* setup wait queue */
1159         init_waitqueue_head(&ctrl->queue);
1160
1161         /* return PCI Controller Info */
1162         ctrl->slot_device_offset = 0;
1163         ctrl->num_slots = 1;
1164         ctrl->first_slot = slot_cap >> 19;
1165         ctrl->ctrlcap = slot_cap & 0x0000007f;
1166
1167         /* Mask Hot-plug Interrupt Enable */
1168         rc = pciehp_readw(ctrl, SLOTCTRL, &temp_word);
1169         if (rc) {
1170                 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
1171                 goto abort_free_ctlr;
1172         }
1173
1174         dbg("%s: SLOTCTRL %x value read %x\n",
1175             __FUNCTION__, ctrl->cap_base + SLOTCTRL, temp_word);
1176         temp_word = (temp_word & ~HP_INTR_ENABLE & ~CMD_CMPL_INTR_ENABLE) | 0x00;
1177
1178         rc = pciehp_writew(ctrl, SLOTCTRL, temp_word);
1179         if (rc) {
1180                 err("%s: Cannot write to SLOTCTRL register\n", __FUNCTION__);
1181                 goto abort_free_ctlr;
1182         }
1183
1184         rc = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
1185         if (rc) {
1186                 err("%s: Cannot read SLOTSTATUS register\n", __FUNCTION__);
1187                 goto abort_free_ctlr;
1188         }
1189
1190         temp_word = 0x1F; /* Clear all events */
1191         rc = pciehp_writew(ctrl, SLOTSTATUS, temp_word);
1192         if (rc) {
1193                 err("%s: Cannot write to SLOTSTATUS register\n", __FUNCTION__);
1194                 goto abort_free_ctlr;
1195         }
1196
1197         if (pciehp_poll_mode) {
1198                 /* Install interrupt polling timer. Start with 10 sec delay */
1199                 init_timer(&ctrl->poll_timer);
1200                 start_int_poll_timer(ctrl, 10);
1201         } else {
1202                 /* Installs the interrupt handler */
1203                 rc = request_irq(ctrl->pci_dev->irq, pcie_isr, IRQF_SHARED,
1204                                  MY_NAME, (void *)ctrl);
1205                 dbg("%s: request_irq %d for hpc%d (returns %d)\n",
1206                     __FUNCTION__, ctrl->pci_dev->irq, ctlr_seq_num, rc);
1207                 if (rc) {
1208                         err("Can't get irq %d for the hotplug controller\n",
1209                             ctrl->pci_dev->irq);
1210                         goto abort_free_ctlr;
1211                 }
1212         }
1213         dbg("pciehp ctrl b:d:f:irq=0x%x:%x:%x:%x\n", pdev->bus->number,
1214                 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn), dev->irq);
1215
1216         rc = pciehp_readw(ctrl, SLOTCTRL, &temp_word);
1217         if (rc) {
1218                 err("%s: Cannot read SLOTCTRL register\n", __FUNCTION__);
1219                 goto abort_free_irq;
1220         }
1221
1222         intr_enable = intr_enable | PRSN_DETECT_ENABLE;
1223
1224         if (ATTN_BUTTN(slot_cap))
1225                 intr_enable = intr_enable | ATTN_BUTTN_ENABLE;
1226         
1227         if (POWER_CTRL(slot_cap))
1228                 intr_enable = intr_enable | PWR_FAULT_DETECT_ENABLE;
1229         
1230         if (MRL_SENS(slot_cap))
1231                 intr_enable = intr_enable | MRL_DETECT_ENABLE;
1232
1233         temp_word = (temp_word & ~intr_enable) | intr_enable; 
1234
1235         if (pciehp_poll_mode) {
1236                 temp_word = (temp_word & ~HP_INTR_ENABLE) | 0x0;
1237         } else {
1238                 temp_word = (temp_word & ~HP_INTR_ENABLE) | HP_INTR_ENABLE;
1239         }
1240
1241         /* Unmask Hot-plug Interrupt Enable for the interrupt notification mechanism case */
1242         rc = pciehp_writew(ctrl, SLOTCTRL, temp_word);
1243         if (rc) {
1244                 err("%s: Cannot write to SLOTCTRL register\n", __FUNCTION__);
1245                 goto abort_free_irq;
1246         }
1247         rc = pciehp_readw(ctrl, SLOTSTATUS, &slot_status);
1248         if (rc) {
1249                 err("%s: Cannot read SLOTSTATUS register\n", __FUNCTION__);
1250                 goto abort_disable_intr;
1251         }
1252         
1253         temp_word =  0x1F; /* Clear all events */
1254         rc = pciehp_writew(ctrl, SLOTSTATUS, temp_word);
1255         if (rc) {
1256                 err("%s: Cannot write to SLOTSTATUS register\n", __FUNCTION__);
1257                 goto abort_disable_intr;
1258         }
1259         
1260         if (pciehp_force) {
1261                 dbg("Bypassing BIOS check for pciehp use on %s\n",
1262                                 pci_name(ctrl->pci_dev));
1263         } else {
1264                 rc = pciehp_get_hp_hw_control_from_firmware(ctrl->pci_dev);
1265                 if (rc)
1266                         goto abort_disable_intr;
1267         }
1268
1269         ctlr_seq_num++;
1270         ctrl->hpc_ops = &pciehp_hpc_ops;
1271
1272         DBG_LEAVE_ROUTINE
1273         return 0;
1274
1275         /* We end up here for the many possible ways to fail this API.  */
1276 abort_disable_intr:
1277         rc = pciehp_readw(ctrl, SLOTCTRL, &temp_word);
1278         if (!rc) {
1279                 temp_word &= ~(intr_enable | HP_INTR_ENABLE);
1280                 rc = pciehp_writew(ctrl, SLOTCTRL, temp_word);
1281         }
1282         if (rc)
1283                 err("%s : disabling interrupts failed\n", __FUNCTION__);
1284
1285 abort_free_irq:
1286         if (pciehp_poll_mode)
1287                 del_timer_sync(&ctrl->poll_timer);
1288         else
1289                 free_irq(ctrl->pci_dev->irq, ctrl);
1290
1291 abort_free_ctlr:
1292         pcie_cap_base = saved_cap_base;
1293
1294         DBG_LEAVE_ROUTINE
1295         return -1;
1296 }