Merge master.kernel.org:/home/rmk/linux-2.6-drvmodel
[cascardo/linux.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/pci.h>
34 #include "../pci.h"
35 #include "pciehp.h"
36
37 #ifdef DEBUG
38 #define DBG_K_TRACE_ENTRY      ((unsigned int)0x00000001)       /* On function entry */
39 #define DBG_K_TRACE_EXIT       ((unsigned int)0x00000002)       /* On function exit */
40 #define DBG_K_INFO             ((unsigned int)0x00000004)       /* Info messages */
41 #define DBG_K_ERROR            ((unsigned int)0x00000008)       /* Error messages */
42 #define DBG_K_TRACE            (DBG_K_TRACE_ENTRY|DBG_K_TRACE_EXIT)
43 #define DBG_K_STANDARD         (DBG_K_INFO|DBG_K_ERROR|DBG_K_TRACE)
44 /* Redefine this flagword to set debug level */
45 #define DEBUG_LEVEL            DBG_K_STANDARD
46
47 #define DEFINE_DBG_BUFFER     char __dbg_str_buf[256];
48
49 #define DBG_PRINT( dbg_flags, args... )              \
50         do {                                             \
51           if ( DEBUG_LEVEL & ( dbg_flags ) )             \
52           {                                              \
53             int len;                                     \
54             len = sprintf( __dbg_str_buf, "%s:%d: %s: ", \
55                   __FILE__, __LINE__, __FUNCTION__ );    \
56             sprintf( __dbg_str_buf + len, args );        \
57             printk( KERN_NOTICE "%s\n", __dbg_str_buf ); \
58           }                                              \
59         } while (0)
60
61 #define DBG_ENTER_ROUTINE       DBG_PRINT (DBG_K_TRACE_ENTRY, "%s", "[Entry]");
62 #define DBG_LEAVE_ROUTINE       DBG_PRINT (DBG_K_TRACE_EXIT, "%s", "[Exit]");
63 #else
64 #define DEFINE_DBG_BUFFER
65 #define DBG_ENTER_ROUTINE
66 #define DBG_LEAVE_ROUTINE
67 #endif                          /* DEBUG */
68
69 struct ctrl_reg {
70         u8 cap_id;
71         u8 nxt_ptr;
72         u16 cap_reg;
73         u32 dev_cap;
74         u16 dev_ctrl;
75         u16 dev_status;
76         u32 lnk_cap;
77         u16 lnk_ctrl;
78         u16 lnk_status;
79         u32 slot_cap;
80         u16 slot_ctrl;
81         u16 slot_status;
82         u16 root_ctrl;
83         u16 rsvp;
84         u32 root_status;
85 } __attribute__ ((packed));
86
87 /* offsets to the controller registers based on the above structure layout */
88 enum ctrl_offsets {
89         PCIECAPID       =       offsetof(struct ctrl_reg, cap_id),
90         NXTCAPPTR       =       offsetof(struct ctrl_reg, nxt_ptr),
91         CAPREG          =       offsetof(struct ctrl_reg, cap_reg),
92         DEVCAP          =       offsetof(struct ctrl_reg, dev_cap),
93         DEVCTRL         =       offsetof(struct ctrl_reg, dev_ctrl),
94         DEVSTATUS       =       offsetof(struct ctrl_reg, dev_status),
95         LNKCAP          =       offsetof(struct ctrl_reg, lnk_cap),
96         LNKCTRL         =       offsetof(struct ctrl_reg, lnk_ctrl),
97         LNKSTATUS       =       offsetof(struct ctrl_reg, lnk_status),
98         SLOTCAP         =       offsetof(struct ctrl_reg, slot_cap),
99         SLOTCTRL        =       offsetof(struct ctrl_reg, slot_ctrl),
100         SLOTSTATUS      =       offsetof(struct ctrl_reg, slot_status),
101         ROOTCTRL        =       offsetof(struct ctrl_reg, root_ctrl),
102         ROOTSTATUS      =       offsetof(struct ctrl_reg, root_status),
103 };
104 static int pcie_cap_base = 0;           /* Base of the PCI Express capability item structure */ 
105
106 #define PCIE_CAP_ID(cb) ( cb + PCIECAPID )
107 #define NXT_CAP_PTR(cb) ( cb + NXTCAPPTR )
108 #define CAP_REG(cb)     ( cb + CAPREG )
109 #define DEV_CAP(cb)     ( cb + DEVCAP )
110 #define DEV_CTRL(cb)    ( cb + DEVCTRL )
111 #define DEV_STATUS(cb)  ( cb + DEVSTATUS )
112 #define LNK_CAP(cb)     ( cb + LNKCAP )
113 #define LNK_CTRL(cb)    ( cb + LNKCTRL )
114 #define LNK_STATUS(cb)  ( cb + LNKSTATUS )
115 #define SLOT_CAP(cb)    ( cb + SLOTCAP )
116 #define SLOT_CTRL(cb)   ( cb + SLOTCTRL )
117 #define SLOT_STATUS(cb) ( cb + SLOTSTATUS )
118 #define ROOT_CTRL(cb)   ( cb + ROOTCTRL )
119 #define ROOT_STATUS(cb) ( cb + ROOTSTATUS )
120
121 #define hp_register_read_word(pdev, reg , value)                \
122         pci_read_config_word(pdev, reg, &value)
123
124 #define hp_register_read_dword(pdev, reg , value)               \
125         pci_read_config_dword(pdev, reg, &value)
126  
127 #define hp_register_write_word(pdev, reg , value)               \
128         pci_write_config_word(pdev, reg, value)
129
130 #define hp_register_dwrite_word(pdev, reg , value)              \
131         pci_write_config_dword(pdev, reg, value)
132
133 /* Field definitions in PCI Express Capabilities Register */
134 #define CAP_VER                 0x000F
135 #define DEV_PORT_TYPE           0x00F0
136 #define SLOT_IMPL               0x0100
137 #define MSG_NUM                 0x3E00
138
139 /* Device or Port Type */
140 #define NAT_ENDPT               0x00
141 #define LEG_ENDPT               0x01
142 #define ROOT_PORT               0x04
143 #define UP_STREAM               0x05
144 #define DN_STREAM               0x06
145 #define PCIE_PCI_BRDG           0x07
146 #define PCI_PCIE_BRDG           0x10
147
148 /* Field definitions in Device Capabilities Register */
149 #define DATTN_BUTTN_PRSN        0x1000
150 #define DATTN_LED_PRSN          0x2000
151 #define DPWR_LED_PRSN           0x4000
152
153 /* Field definitions in Link Capabilities Register */
154 #define MAX_LNK_SPEED           0x000F
155 #define MAX_LNK_WIDTH           0x03F0
156
157 /* Link Width Encoding */
158 #define LNK_X1          0x01
159 #define LNK_X2          0x02
160 #define LNK_X4          0x04    
161 #define LNK_X8          0x08
162 #define LNK_X12         0x0C
163 #define LNK_X16         0x10    
164 #define LNK_X32         0x20
165
166 /*Field definitions of Link Status Register */
167 #define LNK_SPEED       0x000F
168 #define NEG_LINK_WD     0x03F0
169 #define LNK_TRN_ERR     0x0400
170 #define LNK_TRN         0x0800
171 #define SLOT_CLK_CONF   0x1000
172
173 /* Field definitions in Slot Capabilities Register */
174 #define ATTN_BUTTN_PRSN 0x00000001
175 #define PWR_CTRL_PRSN   0x00000002
176 #define MRL_SENS_PRSN   0x00000004
177 #define ATTN_LED_PRSN   0x00000008
178 #define PWR_LED_PRSN    0x00000010
179 #define HP_SUPR_RM_SUP  0x00000020
180 #define HP_CAP          0x00000040
181 #define SLOT_PWR_VALUE  0x000003F8
182 #define SLOT_PWR_LIMIT  0x00000C00
183 #define PSN             0xFFF80000      /* PSN: Physical Slot Number */
184
185 /* Field definitions in Slot Control Register */
186 #define ATTN_BUTTN_ENABLE               0x0001
187 #define PWR_FAULT_DETECT_ENABLE         0x0002
188 #define MRL_DETECT_ENABLE               0x0004
189 #define PRSN_DETECT_ENABLE              0x0008
190 #define CMD_CMPL_INTR_ENABLE            0x0010
191 #define HP_INTR_ENABLE                  0x0020
192 #define ATTN_LED_CTRL                   0x00C0
193 #define PWR_LED_CTRL                    0x0300
194 #define PWR_CTRL                        0x0400
195
196 /* Attention indicator and Power indicator states */
197 #define LED_ON          0x01
198 #define LED_BLINK       0x10
199 #define LED_OFF         0x11
200
201 /* Power Control Command */
202 #define POWER_ON        0
203 #define POWER_OFF       0x0400
204
205 /* Field definitions in Slot Status Register */
206 #define ATTN_BUTTN_PRESSED      0x0001
207 #define PWR_FAULT_DETECTED      0x0002
208 #define MRL_SENS_CHANGED        0x0004
209 #define PRSN_DETECT_CHANGED     0x0008
210 #define CMD_COMPLETED           0x0010
211 #define MRL_STATE               0x0020
212 #define PRSN_STATE              0x0040
213
214 static spinlock_t hpc_event_lock;
215
216 DEFINE_DBG_BUFFER               /* Debug string buffer for entire HPC defined here */
217 static struct php_ctlr_state_s *php_ctlr_list_head; /* HPC state linked list */
218 static int ctlr_seq_num = 0;    /* Controller sequence # */
219 static spinlock_t list_lock;
220
221 static irqreturn_t pcie_isr(int IRQ, void *dev_id, struct pt_regs *regs);
222
223 static void start_int_poll_timer(struct php_ctlr_state_s *php_ctlr, int seconds);
224
225 /* This is the interrupt polling timeout function. */
226 static void int_poll_timeout(unsigned long lphp_ctlr)
227 {
228         struct php_ctlr_state_s *php_ctlr = (struct php_ctlr_state_s *)lphp_ctlr;
229
230         DBG_ENTER_ROUTINE
231
232         if ( !php_ctlr ) {
233                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
234                 return;
235         }
236
237         /* Poll for interrupt events.  regs == NULL => polling */
238         pcie_isr( 0, (void *)php_ctlr, NULL );
239
240         init_timer(&php_ctlr->int_poll_timer);
241
242         if (!pciehp_poll_time)
243                 pciehp_poll_time = 2; /* reset timer to poll in 2 secs if user doesn't specify at module installation*/
244
245         start_int_poll_timer(php_ctlr, pciehp_poll_time);  
246         
247         return;
248 }
249
250 /* This function starts the interrupt polling timer. */
251 static void start_int_poll_timer(struct php_ctlr_state_s *php_ctlr, int seconds)
252 {
253         if (!php_ctlr) {
254                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
255                 return;
256         }
257
258         if ( ( seconds <= 0 ) || ( seconds > 60 ) )
259                 seconds = 2;            /* Clamp to sane value */
260
261         php_ctlr->int_poll_timer.function = &int_poll_timeout;
262         php_ctlr->int_poll_timer.data = (unsigned long)php_ctlr;    /* Instance data */
263         php_ctlr->int_poll_timer.expires = jiffies + seconds * HZ;
264         add_timer(&php_ctlr->int_poll_timer);
265
266         return;
267 }
268
269 static int pcie_write_cmd(struct slot *slot, u16 cmd)
270 {
271         struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
272         int retval = 0;
273         u16 slot_status;
274
275         DBG_ENTER_ROUTINE 
276         
277         if (!php_ctlr) {
278                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
279                 return -1;
280         }
281
282         retval = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS(slot->ctrl->cap_base), slot_status);
283         if (retval) {
284                         err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
285                         return retval;
286                 }
287         
288         if ((slot_status & CMD_COMPLETED) == CMD_COMPLETED ) { 
289                 /* After 1 sec and CMD_COMPLETED still not set, just proceed forward to issue 
290                    the next command according to spec.  Just print out the error message */
291                 dbg("%s : CMD_COMPLETED not clear after 1 sec.\n", __FUNCTION__);
292         }
293
294         retval = hp_register_write_word(php_ctlr->pci_dev, SLOT_CTRL(slot->ctrl->cap_base), cmd | CMD_CMPL_INTR_ENABLE);
295         if (retval) {
296                 err("%s : hp_register_write_word SLOT_CTRL failed\n", __FUNCTION__);
297                 return retval;
298         }
299
300         DBG_LEAVE_ROUTINE 
301         return retval;
302 }
303
304 static int hpc_check_lnk_status(struct controller *ctrl)
305 {
306         struct php_ctlr_state_s *php_ctlr = ctrl->hpc_ctlr_handle;
307         u16 lnk_status;
308         int retval = 0;
309
310         DBG_ENTER_ROUTINE 
311
312         if (!php_ctlr) {
313                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
314                 return -1;
315         }
316         
317         retval = hp_register_read_word(php_ctlr->pci_dev, LNK_STATUS(ctrl->cap_base), lnk_status);
318
319         if (retval) {
320                 err("%s : hp_register_read_word LNK_STATUS failed\n", __FUNCTION__);
321                 return retval;
322         }
323
324         dbg("%s: lnk_status = %x\n", __FUNCTION__, lnk_status);
325         if ( (lnk_status & LNK_TRN) || (lnk_status & LNK_TRN_ERR) || 
326                 !(lnk_status & NEG_LINK_WD)) {
327                 err("%s : Link Training Error occurs \n", __FUNCTION__);
328                 retval = -1;
329                 return retval;
330         }
331
332         DBG_LEAVE_ROUTINE 
333         return retval;
334 }
335
336
337 static int hpc_get_attention_status(struct slot *slot, u8 *status)
338 {
339         struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
340         u16 slot_ctrl;
341         u8 atten_led_state;
342         int retval = 0;
343         
344         DBG_ENTER_ROUTINE 
345
346         if (!php_ctlr) {
347                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
348                 return -1;
349         }
350
351         retval = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL(slot->ctrl->cap_base), slot_ctrl);
352
353         if (retval) {
354                 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
355                 return retval;
356         }
357
358         dbg("%s: SLOT_CTRL %x, value read %x\n", __FUNCTION__,SLOT_CTRL(slot->ctrl->cap_base), slot_ctrl);
359
360         atten_led_state = (slot_ctrl & ATTN_LED_CTRL) >> 6;
361
362         switch (atten_led_state) {
363         case 0:
364                 *status = 0xFF; /* Reserved */
365                 break;
366         case 1:
367                 *status = 1;    /* On */
368                 break;
369         case 2:
370                 *status = 2;    /* Blink */
371                 break;
372         case 3:
373                 *status = 0;    /* Off */
374                 break;
375         default:
376                 *status = 0xFF;
377                 break;
378         }
379
380         DBG_LEAVE_ROUTINE 
381         return 0;
382 }
383
384 static int hpc_get_power_status(struct slot * slot, u8 *status)
385 {
386         struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
387         u16 slot_ctrl;
388         u8 pwr_state;
389         int     retval = 0;
390         
391         DBG_ENTER_ROUTINE 
392
393         if (!php_ctlr) {
394                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
395                 return -1;
396         }
397
398         retval = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL(slot->ctrl->cap_base), slot_ctrl);
399
400         if (retval) {
401                 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
402                 return retval;
403         }
404         dbg("%s: SLOT_CTRL %x value read %x\n", __FUNCTION__, SLOT_CTRL(slot->ctrl->cap_base), slot_ctrl);
405
406         pwr_state = (slot_ctrl & PWR_CTRL) >> 10;
407
408         switch (pwr_state) {
409         case 0:
410                 *status = 1;
411                 break;
412         case 1:
413                 *status = 0;    
414                 break;
415         default:
416                 *status = 0xFF;
417                 break;
418         }
419
420         DBG_LEAVE_ROUTINE 
421         return retval;
422 }
423
424
425 static int hpc_get_latch_status(struct slot *slot, u8 *status)
426 {
427         struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
428         u16 slot_status;
429         int retval = 0;
430
431         DBG_ENTER_ROUTINE 
432
433         if (!php_ctlr) {
434                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
435                 return -1;
436         }
437
438         retval = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS(slot->ctrl->cap_base), slot_status);
439
440         if (retval) {
441                 err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
442                 return retval;
443         }
444
445         *status = (((slot_status & MRL_STATE) >> 5) == 0) ? 0 : 1;  
446
447         DBG_LEAVE_ROUTINE 
448         return 0;
449 }
450
451 static int hpc_get_adapter_status(struct slot *slot, u8 *status)
452 {
453         struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
454         u16 slot_status;
455         u8 card_state;
456         int retval = 0;
457
458         DBG_ENTER_ROUTINE 
459
460         if (!php_ctlr) {
461                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
462                 return -1;
463         }
464
465         retval = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS(slot->ctrl->cap_base), slot_status);
466
467         if (retval) {
468                 err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
469                 return retval;
470         }
471         card_state = (u8)((slot_status & PRSN_STATE) >> 6);
472         *status = (card_state == 1) ? 1 : 0;
473
474         DBG_LEAVE_ROUTINE 
475         return 0;
476 }
477
478 static int hpc_query_power_fault(struct slot * slot)
479 {
480         struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
481         u16 slot_status;
482         u8 pwr_fault;
483         int retval = 0;
484
485         DBG_ENTER_ROUTINE 
486
487         if (!php_ctlr) {
488                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
489                 return -1;
490         }
491
492         retval = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS(slot->ctrl->cap_base), slot_status);
493
494         if (retval) {
495                 err("%s : Cannot check for power fault\n", __FUNCTION__);
496                 return retval;
497         }
498         pwr_fault = (u8)((slot_status & PWR_FAULT_DETECTED) >> 1);
499         
500         DBG_LEAVE_ROUTINE
501         return pwr_fault;
502 }
503
504 static int hpc_set_attention_status(struct slot *slot, u8 value)
505 {
506         struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
507         u16 slot_cmd = 0;
508         u16 slot_ctrl;
509         int rc = 0;
510
511         DBG_ENTER_ROUTINE
512
513         if (!php_ctlr) {
514                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
515                 return -1;
516         }
517
518         if (slot->hp_slot >= php_ctlr->num_slots) {
519                 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
520                 return -1;
521         }
522         rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL(slot->ctrl->cap_base), slot_ctrl);
523
524         if (rc) {
525                 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
526                 return rc;
527         }
528
529         switch (value) {
530                 case 0 :        /* turn off */
531                         slot_cmd = (slot_ctrl & ~ATTN_LED_CTRL) | 0x00C0;
532                         break;
533                 case 1:         /* turn on */
534                         slot_cmd = (slot_ctrl & ~ATTN_LED_CTRL) | 0x0040;
535                         break;
536                 case 2:         /* turn blink */
537                         slot_cmd = (slot_ctrl & ~ATTN_LED_CTRL) | 0x0080;
538                         break;
539                 default:
540                         return -1;
541         }
542         if (!pciehp_poll_mode)
543                 slot_cmd = slot_cmd | HP_INTR_ENABLE; 
544
545         pcie_write_cmd(slot, slot_cmd);
546         dbg("%s: SLOT_CTRL %x write cmd %x\n", __FUNCTION__, SLOT_CTRL(slot->ctrl->cap_base), slot_cmd);
547         
548         DBG_LEAVE_ROUTINE
549         return rc;
550 }
551
552
553 static void hpc_set_green_led_on(struct slot *slot)
554 {
555         struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
556         u16 slot_cmd;
557         u16 slot_ctrl;
558         int rc = 0;
559         
560         DBG_ENTER_ROUTINE
561
562         if (!php_ctlr) {
563                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
564                 return ;
565         }
566
567         if (slot->hp_slot >= php_ctlr->num_slots) {
568                 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
569                 return ;
570         }
571
572         rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL(slot->ctrl->cap_base), slot_ctrl);
573
574         if (rc) {
575                 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
576                 return;
577         }
578         slot_cmd = (slot_ctrl & ~PWR_LED_CTRL) | 0x0100;
579         if (!pciehp_poll_mode)
580                 slot_cmd = slot_cmd | HP_INTR_ENABLE; 
581
582         pcie_write_cmd(slot, slot_cmd);
583
584         dbg("%s: SLOT_CTRL %x write cmd %x\n",__FUNCTION__, SLOT_CTRL(slot->ctrl->cap_base), slot_cmd);
585         DBG_LEAVE_ROUTINE
586         return;
587 }
588
589 static void hpc_set_green_led_off(struct slot *slot)
590 {
591         struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
592         u16 slot_cmd;
593         u16 slot_ctrl;
594         int rc = 0;
595
596         DBG_ENTER_ROUTINE
597
598         if (!php_ctlr) {
599                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
600                 return ;
601         }
602
603         if (slot->hp_slot >= php_ctlr->num_slots) {
604                 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
605                 return ;
606         }
607
608         rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL(slot->ctrl->cap_base), slot_ctrl);
609
610         if (rc) {
611                 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
612                 return;
613         }
614
615         slot_cmd = (slot_ctrl & ~PWR_LED_CTRL) | 0x0300;
616
617         if (!pciehp_poll_mode)
618                 slot_cmd = slot_cmd | HP_INTR_ENABLE; 
619         pcie_write_cmd(slot, slot_cmd);
620         dbg("%s: SLOT_CTRL %x write cmd %x\n", __FUNCTION__, SLOT_CTRL(slot->ctrl->cap_base), slot_cmd);
621
622         DBG_LEAVE_ROUTINE
623         return;
624 }
625
626 static void hpc_set_green_led_blink(struct slot *slot)
627 {
628         struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
629         u16 slot_cmd;
630         u16 slot_ctrl;
631         int rc = 0; 
632         
633         DBG_ENTER_ROUTINE
634
635         if (!php_ctlr) {
636                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
637                 return ;
638         }
639
640         if (slot->hp_slot >= php_ctlr->num_slots) {
641                 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
642                 return ;
643         }
644
645         rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL(slot->ctrl->cap_base), slot_ctrl);
646
647         if (rc) {
648                 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
649                 return;
650         }
651
652         slot_cmd = (slot_ctrl & ~PWR_LED_CTRL) | 0x0200;
653
654         if (!pciehp_poll_mode)
655                 slot_cmd = slot_cmd | HP_INTR_ENABLE; 
656         pcie_write_cmd(slot, slot_cmd);
657
658         dbg("%s: SLOT_CTRL %x write cmd %x\n",__FUNCTION__, SLOT_CTRL(slot->ctrl->cap_base), slot_cmd);
659         DBG_LEAVE_ROUTINE
660         return;
661 }
662
663 int pcie_get_ctlr_slot_config(struct controller *ctrl,
664         int *num_ctlr_slots,    /* number of slots in this HPC; only 1 in PCIE  */      
665         int *first_device_num,  /* PCI dev num of the first slot in this PCIE   */
666         int *physical_slot_num, /* phy slot num of the first slot in this PCIE  */
667         u8 *ctrlcap)
668 {
669         struct php_ctlr_state_s *php_ctlr = ctrl->hpc_ctlr_handle;
670         u32 slot_cap;
671         int rc = 0;
672         
673         DBG_ENTER_ROUTINE 
674
675         if (!php_ctlr) {
676                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
677                 return -1;
678         }
679
680         *first_device_num = 0;
681         *num_ctlr_slots = 1; 
682
683         rc = hp_register_read_dword(php_ctlr->pci_dev, SLOT_CAP(ctrl->cap_base), slot_cap);
684
685         if (rc) {
686                 err("%s : hp_register_read_dword SLOT_CAP failed\n", __FUNCTION__);
687                 return -1;
688         }
689         
690         *physical_slot_num = slot_cap >> 19;
691         dbg("%s: PSN %d \n", __FUNCTION__, *physical_slot_num);
692         
693         *ctrlcap = slot_cap & 0x0000007f;
694
695         DBG_LEAVE_ROUTINE 
696         return 0;
697 }
698
699 static void hpc_release_ctlr(struct controller *ctrl)
700 {
701         struct php_ctlr_state_s *php_ctlr = ctrl->hpc_ctlr_handle;
702         struct php_ctlr_state_s *p, *p_prev;
703
704         DBG_ENTER_ROUTINE 
705
706         if (!php_ctlr) {
707                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
708                 return ;
709         }
710
711         if (pciehp_poll_mode) {
712             del_timer(&php_ctlr->int_poll_timer);
713         } else {        
714                 if (php_ctlr->irq) {
715                         free_irq(php_ctlr->irq, ctrl);
716                         php_ctlr->irq = 0;
717                         if (!pcie_mch_quirk) 
718                                 pci_disable_msi(php_ctlr->pci_dev);
719                 }
720         }
721         if (php_ctlr->pci_dev) 
722                 php_ctlr->pci_dev = NULL;
723
724         spin_lock(&list_lock);
725         p = php_ctlr_list_head;
726         p_prev = NULL;
727         while (p) {
728                 if (p == php_ctlr) {
729                         if (p_prev)
730                                 p_prev->pnext = p->pnext;
731                         else
732                                 php_ctlr_list_head = p->pnext;
733                         break;
734                 } else {
735                         p_prev = p;
736                         p = p->pnext;
737                 }
738         }
739         spin_unlock(&list_lock);
740
741         kfree(php_ctlr);
742
743         DBG_LEAVE_ROUTINE
744                           
745 }
746
747 static int hpc_power_on_slot(struct slot * slot)
748 {
749         struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
750         u16 slot_cmd;
751         u16 slot_ctrl;
752
753         int retval = 0;
754
755         DBG_ENTER_ROUTINE 
756
757         if (!php_ctlr) {
758                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
759                 return -1;
760         }
761
762         dbg("%s: slot->hp_slot %x\n", __FUNCTION__, slot->hp_slot);
763         if (slot->hp_slot >= php_ctlr->num_slots) {
764                 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
765                 return -1;
766         }
767
768         retval = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL(slot->ctrl->cap_base), slot_ctrl);
769
770         if (retval) {
771                 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
772                 return retval;
773         }
774
775         slot_cmd = (slot_ctrl & ~PWR_CTRL) | POWER_ON;
776
777         if (!pciehp_poll_mode)
778                 slot_cmd = slot_cmd | HP_INTR_ENABLE; 
779
780         retval = pcie_write_cmd(slot, slot_cmd);
781
782         if (retval) {
783                 err("%s: Write %x command failed!\n", __FUNCTION__, slot_cmd);
784                 return -1;
785         }
786         dbg("%s: SLOT_CTRL %x write cmd %x\n",__FUNCTION__, SLOT_CTRL(slot->ctrl->cap_base), slot_cmd);
787
788         DBG_LEAVE_ROUTINE
789
790         return retval;
791 }
792
793 static int hpc_power_off_slot(struct slot * slot)
794 {
795         struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
796         u16 slot_cmd;
797         u16 slot_ctrl;
798
799         int retval = 0;
800
801         DBG_ENTER_ROUTINE 
802
803         if (!php_ctlr) {
804                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
805                 return -1;
806         }
807
808         dbg("%s: slot->hp_slot %x\n", __FUNCTION__, slot->hp_slot);
809         slot->hp_slot = 0;
810         if (slot->hp_slot >= php_ctlr->num_slots) {
811                 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
812                 return -1;
813         }
814         retval = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL(slot->ctrl->cap_base), slot_ctrl);
815
816         if (retval) {
817                 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
818                 return retval;
819         }
820
821         slot_cmd = (slot_ctrl & ~PWR_CTRL) | POWER_OFF;
822
823         if (!pciehp_poll_mode)
824                 slot_cmd = slot_cmd | HP_INTR_ENABLE; 
825
826         retval = pcie_write_cmd(slot, slot_cmd);
827
828         if (retval) {
829                 err("%s: Write command failed!\n", __FUNCTION__);
830                 return -1;
831         }
832         dbg("%s: SLOT_CTRL %x write cmd %x\n",__FUNCTION__, SLOT_CTRL(slot->ctrl->cap_base), slot_cmd);
833
834         DBG_LEAVE_ROUTINE
835
836         return retval;
837 }
838
839 static irqreturn_t pcie_isr(int IRQ, void *dev_id, struct pt_regs *regs)
840 {
841         struct controller *ctrl = NULL;
842         struct php_ctlr_state_s *php_ctlr;
843         u8 schedule_flag = 0;
844         u16 slot_status, intr_detect, intr_loc;
845         u16 temp_word;
846         int hp_slot = 0;        /* only 1 slot per PCI Express port */
847         int rc = 0;
848
849         if (!dev_id)
850                 return IRQ_NONE;
851
852         if (!pciehp_poll_mode) { 
853                 ctrl = dev_id;
854                 php_ctlr = ctrl->hpc_ctlr_handle;
855         } else {
856                 php_ctlr = dev_id;
857                 ctrl = (struct controller *)php_ctlr->callback_instance_id;
858         }
859
860         if (!ctrl) {
861                 dbg("%s: dev_id %p ctlr == NULL\n", __FUNCTION__, (void*) dev_id);
862                 return IRQ_NONE;
863         }
864         
865         if (!php_ctlr) {
866                 dbg("%s: php_ctlr == NULL\n", __FUNCTION__);
867                 return IRQ_NONE;
868         }
869
870         rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), slot_status);
871         if (rc) {
872                 err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
873                 return IRQ_NONE;
874         }
875
876         intr_detect = ( ATTN_BUTTN_PRESSED | PWR_FAULT_DETECTED | MRL_SENS_CHANGED |
877                                         PRSN_DETECT_CHANGED | CMD_COMPLETED );
878
879         intr_loc = slot_status & intr_detect;
880
881         /* Check to see if it was our interrupt */
882         if ( !intr_loc )
883                 return IRQ_NONE;
884
885         dbg("%s: intr_loc %x\n", __FUNCTION__, intr_loc);
886         /* Mask Hot-plug Interrupt Enable */
887         if (!pciehp_poll_mode) {
888                 rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL(ctrl->cap_base), temp_word);
889                 if (rc) {
890                         err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
891                         return IRQ_NONE;
892                 }
893
894                 dbg("%s: hp_register_read_word SLOT_CTRL with value %x\n", __FUNCTION__, temp_word);
895                 temp_word = (temp_word & ~HP_INTR_ENABLE & ~CMD_CMPL_INTR_ENABLE) | 0x00;
896
897                 rc = hp_register_write_word(php_ctlr->pci_dev, SLOT_CTRL(ctrl->cap_base), temp_word);
898                 if (rc) {
899                         err("%s : hp_register_write_word SLOT_CTRL failed\n", __FUNCTION__);
900                         return IRQ_NONE;
901                 }
902                 
903                 rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), slot_status);
904                 if (rc) {
905                         err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
906                         return IRQ_NONE;
907                 }
908                 dbg("%s: hp_register_read_word SLOT_STATUS with value %x\n", __FUNCTION__, slot_status); 
909                 
910                 /* Clear command complete interrupt caused by this write */
911                 temp_word = 0x1f;
912                 rc = hp_register_write_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), temp_word);
913                 if (rc) {
914                         err("%s : hp_register_write_word SLOT_STATUS failed\n", __FUNCTION__);
915                         return IRQ_NONE;
916                 }
917         }
918         
919         if (intr_loc & CMD_COMPLETED) {
920                 /* 
921                  * Command Complete Interrupt Pending 
922                  */
923                 wake_up_interruptible(&ctrl->queue);
924         }
925
926         if ((php_ctlr->switch_change_callback) && (intr_loc & MRL_SENS_CHANGED))
927                 schedule_flag += php_ctlr->switch_change_callback(
928                         hp_slot, php_ctlr->callback_instance_id);
929         if ((php_ctlr->attention_button_callback) && (intr_loc & ATTN_BUTTN_PRESSED))
930                 schedule_flag += php_ctlr->attention_button_callback(
931                         hp_slot, php_ctlr->callback_instance_id);
932         if ((php_ctlr->presence_change_callback) && (intr_loc & PRSN_DETECT_CHANGED))
933                 schedule_flag += php_ctlr->presence_change_callback(
934                         hp_slot , php_ctlr->callback_instance_id);
935         if ((php_ctlr->power_fault_callback) && (intr_loc & PWR_FAULT_DETECTED))
936                 schedule_flag += php_ctlr->power_fault_callback(
937                         hp_slot, php_ctlr->callback_instance_id);
938
939         /* Clear all events after serving them */
940         temp_word = 0x1F;
941         rc = hp_register_write_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), temp_word);
942         if (rc) {
943                 err("%s : hp_register_write_word SLOT_STATUS failed\n", __FUNCTION__);
944                 return IRQ_NONE;
945         }
946         /* Unmask Hot-plug Interrupt Enable */
947         if (!pciehp_poll_mode) {
948                 rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL(ctrl->cap_base), temp_word);
949                 if (rc) {
950                         err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
951                         return IRQ_NONE;
952                 }
953
954                 dbg("%s: Unmask Hot-plug Interrupt Enable\n", __FUNCTION__);
955                 temp_word = (temp_word & ~HP_INTR_ENABLE) | HP_INTR_ENABLE;
956
957                 rc = hp_register_write_word(php_ctlr->pci_dev, SLOT_CTRL(ctrl->cap_base), temp_word);
958                 if (rc) {
959                         err("%s : hp_register_write_word SLOT_CTRL failed\n", __FUNCTION__);
960                         return IRQ_NONE;
961                 }
962         
963                 rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), slot_status);
964                 if (rc) {
965                         err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
966                         return IRQ_NONE;
967                 }
968                 
969                 /* Clear command complete interrupt caused by this write */
970                 temp_word = 0x1F;
971                 rc = hp_register_write_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), temp_word);
972                 if (rc) {
973                         err("%s : hp_register_write_word SLOT_STATUS failed\n", __FUNCTION__);
974                         return IRQ_NONE;
975                 }
976                 dbg("%s: hp_register_write_word SLOT_STATUS with value %x\n", __FUNCTION__, temp_word); 
977         }
978         
979         return IRQ_HANDLED;
980 }
981
982 static int hpc_get_max_lnk_speed (struct slot *slot, enum pci_bus_speed *value)
983 {
984         struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
985         enum pcie_link_speed lnk_speed;
986         u32     lnk_cap;
987         int retval = 0;
988
989         DBG_ENTER_ROUTINE 
990
991         if (!php_ctlr) {
992                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
993                 return -1;
994         }
995
996         if (slot->hp_slot >= php_ctlr->num_slots) {
997                 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
998                 return -1;
999         }
1000
1001         retval = hp_register_read_dword(php_ctlr->pci_dev, LNK_CAP(slot->ctrl->cap_base), lnk_cap);
1002
1003         if (retval) {
1004                 err("%s : hp_register_read_dword  LNK_CAP failed\n", __FUNCTION__);
1005                 return retval;
1006         }
1007
1008         switch (lnk_cap & 0x000F) {
1009         case 1:
1010                 lnk_speed = PCIE_2PT5GB;
1011                 break;
1012         default:
1013                 lnk_speed = PCIE_LNK_SPEED_UNKNOWN;
1014                 break;
1015         }
1016
1017         *value = lnk_speed;
1018         dbg("Max link speed = %d\n", lnk_speed);
1019         DBG_LEAVE_ROUTINE 
1020         return retval;
1021 }
1022
1023 static int hpc_get_max_lnk_width (struct slot *slot, enum pcie_link_width *value)
1024 {
1025         struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
1026         enum pcie_link_width lnk_wdth;
1027         u32     lnk_cap;
1028         int retval = 0;
1029
1030         DBG_ENTER_ROUTINE 
1031
1032         if (!php_ctlr) {
1033                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
1034                 return -1;
1035         }
1036
1037         if (slot->hp_slot >= php_ctlr->num_slots) {
1038                 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
1039                 return -1;
1040         }
1041
1042         retval = hp_register_read_dword(php_ctlr->pci_dev, LNK_CAP(slot->ctrl->cap_base), lnk_cap);
1043
1044         if (retval) {
1045                 err("%s : hp_register_read_dword  LNK_CAP failed\n", __FUNCTION__);
1046                 return retval;
1047         }
1048
1049         switch ((lnk_cap & 0x03F0) >> 4){
1050         case 0:
1051                 lnk_wdth = PCIE_LNK_WIDTH_RESRV;
1052                 break;
1053         case 1:
1054                 lnk_wdth = PCIE_LNK_X1;
1055                 break;
1056         case 2:
1057                 lnk_wdth = PCIE_LNK_X2;
1058                 break;
1059         case 4:
1060                 lnk_wdth = PCIE_LNK_X4;
1061                 break;
1062         case 8:
1063                 lnk_wdth = PCIE_LNK_X8;
1064                 break;
1065         case 12:
1066                 lnk_wdth = PCIE_LNK_X12;
1067                 break;
1068         case 16:
1069                 lnk_wdth = PCIE_LNK_X16;
1070                 break;
1071         case 32:
1072                 lnk_wdth = PCIE_LNK_X32;
1073                 break;
1074         default:
1075                 lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
1076                 break;
1077         }
1078
1079         *value = lnk_wdth;
1080         dbg("Max link width = %d\n", lnk_wdth);
1081         DBG_LEAVE_ROUTINE 
1082         return retval;
1083 }
1084
1085 static int hpc_get_cur_lnk_speed (struct slot *slot, enum pci_bus_speed *value)
1086 {
1087         struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
1088         enum pcie_link_speed lnk_speed = PCI_SPEED_UNKNOWN;
1089         int retval = 0;
1090         u16 lnk_status;
1091
1092         DBG_ENTER_ROUTINE 
1093
1094         if (!php_ctlr) {
1095                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
1096                 return -1;
1097         }
1098
1099         if (slot->hp_slot >= php_ctlr->num_slots) {
1100                 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
1101                 return -1;
1102         }
1103
1104         retval = hp_register_read_word(php_ctlr->pci_dev, LNK_STATUS(slot->ctrl->cap_base), lnk_status);
1105
1106         if (retval) {
1107                 err("%s : hp_register_read_word LNK_STATUS failed\n", __FUNCTION__);
1108                 return retval;
1109         }
1110
1111         switch (lnk_status & 0x0F) {
1112         case 1:
1113                 lnk_speed = PCIE_2PT5GB;
1114                 break;
1115         default:
1116                 lnk_speed = PCIE_LNK_SPEED_UNKNOWN;
1117                 break;
1118         }
1119
1120         *value = lnk_speed;
1121         dbg("Current link speed = %d\n", lnk_speed);
1122         DBG_LEAVE_ROUTINE 
1123         return retval;
1124 }
1125
1126 static int hpc_get_cur_lnk_width (struct slot *slot, enum pcie_link_width *value)
1127 {
1128         struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
1129         enum pcie_link_width lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
1130         int retval = 0;
1131         u16 lnk_status;
1132
1133         DBG_ENTER_ROUTINE 
1134
1135         if (!php_ctlr) {
1136                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
1137                 return -1;
1138         }
1139
1140         if (slot->hp_slot >= php_ctlr->num_slots) {
1141                 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
1142                 return -1;
1143         }
1144
1145         retval = hp_register_read_word(php_ctlr->pci_dev, LNK_STATUS(slot->ctrl->cap_base), lnk_status);
1146
1147         if (retval) {
1148                 err("%s : hp_register_read_word LNK_STATUS failed\n", __FUNCTION__);
1149                 return retval;
1150         }
1151         
1152         switch ((lnk_status & 0x03F0) >> 4){
1153         case 0:
1154                 lnk_wdth = PCIE_LNK_WIDTH_RESRV;
1155                 break;
1156         case 1:
1157                 lnk_wdth = PCIE_LNK_X1;
1158                 break;
1159         case 2:
1160                 lnk_wdth = PCIE_LNK_X2;
1161                 break;
1162         case 4:
1163                 lnk_wdth = PCIE_LNK_X4;
1164                 break;
1165         case 8:
1166                 lnk_wdth = PCIE_LNK_X8;
1167                 break;
1168         case 12:
1169                 lnk_wdth = PCIE_LNK_X12;
1170                 break;
1171         case 16:
1172                 lnk_wdth = PCIE_LNK_X16;
1173                 break;
1174         case 32:
1175                 lnk_wdth = PCIE_LNK_X32;
1176                 break;
1177         default:
1178                 lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
1179                 break;
1180         }
1181
1182         *value = lnk_wdth;
1183         dbg("Current link width = %d\n", lnk_wdth);
1184         DBG_LEAVE_ROUTINE 
1185         return retval;
1186 }
1187
1188 static struct hpc_ops pciehp_hpc_ops = {
1189         .power_on_slot                  = hpc_power_on_slot,
1190         .power_off_slot                 = hpc_power_off_slot,
1191         .set_attention_status           = hpc_set_attention_status,
1192         .get_power_status               = hpc_get_power_status,
1193         .get_attention_status           = hpc_get_attention_status,
1194         .get_latch_status               = hpc_get_latch_status,
1195         .get_adapter_status             = hpc_get_adapter_status,
1196
1197         .get_max_bus_speed              = hpc_get_max_lnk_speed,
1198         .get_cur_bus_speed              = hpc_get_cur_lnk_speed,
1199         .get_max_lnk_width              = hpc_get_max_lnk_width,
1200         .get_cur_lnk_width              = hpc_get_cur_lnk_width,
1201         
1202         .query_power_fault              = hpc_query_power_fault,
1203         .green_led_on                   = hpc_set_green_led_on,
1204         .green_led_off                  = hpc_set_green_led_off,
1205         .green_led_blink                = hpc_set_green_led_blink,
1206         
1207         .release_ctlr                   = hpc_release_ctlr,
1208         .check_lnk_status               = hpc_check_lnk_status,
1209 };
1210
1211 int pcie_init(struct controller * ctrl, struct pcie_device *dev)
1212 {
1213         struct php_ctlr_state_s *php_ctlr, *p;
1214         void *instance_id = ctrl;
1215         int rc;
1216         static int first = 1;
1217         u16 temp_word;
1218         u16 cap_reg;
1219         u16 intr_enable = 0;
1220         u32 slot_cap;
1221         int cap_base, saved_cap_base;
1222         u16 slot_status, slot_ctrl;
1223         struct pci_dev *pdev;
1224
1225         DBG_ENTER_ROUTINE
1226         
1227         spin_lock_init(&list_lock);     
1228         php_ctlr = (struct php_ctlr_state_s *) kmalloc(sizeof(struct php_ctlr_state_s), GFP_KERNEL);
1229
1230         if (!php_ctlr) {        /* allocate controller state data */
1231                 err("%s: HPC controller memory allocation error!\n", __FUNCTION__);
1232                 goto abort;
1233         }
1234
1235         memset(php_ctlr, 0, sizeof(struct php_ctlr_state_s));
1236         
1237         pdev = dev->port;
1238         php_ctlr->pci_dev = pdev;       /* save pci_dev in context */
1239
1240         dbg("%s: hotplug controller vendor id 0x%x device id 0x%x\n",
1241                         __FUNCTION__, pdev->vendor, pdev->device);
1242
1243         saved_cap_base = pcie_cap_base;
1244
1245         if ((cap_base = pci_find_capability(pdev, PCI_CAP_ID_EXP)) == 0) {
1246                 dbg("%s: Can't find PCI_CAP_ID_EXP (0x10)\n", __FUNCTION__);
1247                 goto abort_free_ctlr;
1248         }
1249
1250         ctrl->cap_base = cap_base;
1251
1252         dbg("%s: pcie_cap_base %x\n", __FUNCTION__, pcie_cap_base);
1253
1254         rc = hp_register_read_word(pdev, CAP_REG(ctrl->cap_base), cap_reg);
1255         if (rc) {
1256                 err("%s : hp_register_read_word CAP_REG failed\n", __FUNCTION__);
1257                 goto abort_free_ctlr;
1258         }
1259         dbg("%s: CAP_REG offset %x cap_reg %x\n", __FUNCTION__, CAP_REG(ctrl->cap_base), cap_reg);
1260
1261         if (((cap_reg & SLOT_IMPL) == 0) || (((cap_reg & DEV_PORT_TYPE) != 0x0040)
1262                 && ((cap_reg & DEV_PORT_TYPE) != 0x0060))) {
1263                 dbg("%s : This is not a root port or the port is not connected to a slot\n", __FUNCTION__);
1264                 goto abort_free_ctlr;
1265         }
1266
1267         rc = hp_register_read_dword(php_ctlr->pci_dev, SLOT_CAP(ctrl->cap_base), slot_cap);
1268         if (rc) {
1269                 err("%s : hp_register_read_word CAP_REG failed\n", __FUNCTION__);
1270                 goto abort_free_ctlr;
1271         }
1272         dbg("%s: SLOT_CAP offset %x slot_cap %x\n", __FUNCTION__, SLOT_CAP(ctrl->cap_base), slot_cap);
1273
1274         if (!(slot_cap & HP_CAP)) {
1275                 dbg("%s : This slot is not hot-plug capable\n", __FUNCTION__);
1276                 goto abort_free_ctlr;
1277         }
1278         /* For debugging purpose */
1279         rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), slot_status);
1280         if (rc) {
1281                 err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
1282                 goto abort_free_ctlr;
1283         }
1284         dbg("%s: SLOT_STATUS offset %x slot_status %x\n", __FUNCTION__, SLOT_STATUS(ctrl->cap_base), slot_status);
1285
1286         rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL(ctrl->cap_base), slot_ctrl);
1287         if (rc) {
1288                 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
1289                 goto abort_free_ctlr;
1290         }
1291         dbg("%s: SLOT_CTRL offset %x slot_ctrl %x\n", __FUNCTION__, SLOT_CTRL(ctrl->cap_base), slot_ctrl);
1292
1293         if (first) {
1294                 spin_lock_init(&hpc_event_lock);
1295                 first = 0;
1296         }
1297
1298         for ( rc = 0; rc < DEVICE_COUNT_RESOURCE; rc++)
1299                 if (pci_resource_len(pdev, rc) > 0)
1300                         dbg("pci resource[%d] start=0x%lx(len=0x%lx)\n", rc,
1301                                 pci_resource_start(pdev, rc), pci_resource_len(pdev, rc));
1302
1303         info("HPC vendor_id %x device_id %x ss_vid %x ss_did %x\n", pdev->vendor, pdev->device, 
1304                 pdev->subsystem_vendor, pdev->subsystem_device);
1305
1306         if (pci_enable_device(pdev))
1307                 goto abort_free_ctlr;
1308         
1309         init_MUTEX(&ctrl->crit_sect);
1310         /* setup wait queue */
1311         init_waitqueue_head(&ctrl->queue);
1312
1313         /* find the IRQ */
1314         php_ctlr->irq = dev->irq;
1315
1316         /* Save interrupt callback info */
1317         php_ctlr->attention_button_callback = pciehp_handle_attention_button;
1318         php_ctlr->switch_change_callback = pciehp_handle_switch_change;
1319         php_ctlr->presence_change_callback = pciehp_handle_presence_change;
1320         php_ctlr->power_fault_callback = pciehp_handle_power_fault;
1321         php_ctlr->callback_instance_id = instance_id;
1322
1323         /* return PCI Controller Info */
1324         php_ctlr->slot_device_offset = 0;
1325         php_ctlr->num_slots = 1;
1326
1327         /* Mask Hot-plug Interrupt Enable */
1328         rc = hp_register_read_word(pdev, SLOT_CTRL(ctrl->cap_base), temp_word);
1329         if (rc) {
1330                 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
1331                 goto abort_free_ctlr;
1332         }
1333
1334         dbg("%s: SLOT_CTRL %x value read %x\n", __FUNCTION__, SLOT_CTRL(ctrl->cap_base), temp_word);
1335         temp_word = (temp_word & ~HP_INTR_ENABLE & ~CMD_CMPL_INTR_ENABLE) | 0x00;
1336
1337         rc = hp_register_write_word(pdev, SLOT_CTRL(ctrl->cap_base), temp_word);
1338         if (rc) {
1339                 err("%s : hp_register_write_word SLOT_CTRL failed\n", __FUNCTION__);
1340                 goto abort_free_ctlr;
1341         }
1342
1343         rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), slot_status);
1344         if (rc) {
1345                 err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
1346                 goto abort_free_ctlr;
1347         }
1348
1349         temp_word = 0x1F; /* Clear all events */
1350         rc = hp_register_write_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), temp_word);
1351         if (rc) {
1352                 err("%s : hp_register_write_word SLOT_STATUS failed\n", __FUNCTION__);
1353                 goto abort_free_ctlr;
1354         }
1355
1356         if (pciehp_poll_mode)  {/* Install interrupt polling code */
1357                 /* Install and start the interrupt polling timer */
1358                 init_timer(&php_ctlr->int_poll_timer);
1359                 start_int_poll_timer( php_ctlr, 10 );   /* start with 10 second delay */
1360         } else {
1361                 /* Installs the interrupt handler */
1362                 rc = request_irq(php_ctlr->irq, pcie_isr, SA_SHIRQ, MY_NAME, (void *) ctrl);
1363                 dbg("%s: request_irq %d for hpc%d (returns %d)\n", __FUNCTION__, php_ctlr->irq, ctlr_seq_num, rc);
1364                 if (rc) {
1365                         err("Can't get irq %d for the hotplug controller\n", php_ctlr->irq);
1366                         goto abort_free_ctlr;
1367                 }
1368         }
1369
1370         dbg("pciehp ctrl b:d:f:irq=0x%x:%x:%x:%x\n", pdev->bus->number,
1371                 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn), dev->irq);
1372
1373         rc = hp_register_read_word(pdev, SLOT_CTRL(ctrl->cap_base), temp_word);
1374         if (rc) {
1375                 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
1376                 goto abort_free_ctlr;
1377         }
1378
1379         intr_enable = intr_enable | PRSN_DETECT_ENABLE;
1380
1381         if (ATTN_BUTTN(slot_cap))
1382                 intr_enable = intr_enable | ATTN_BUTTN_ENABLE;
1383         
1384         if (POWER_CTRL(slot_cap))
1385                 intr_enable = intr_enable | PWR_FAULT_DETECT_ENABLE;
1386         
1387         if (MRL_SENS(slot_cap))
1388                 intr_enable = intr_enable | MRL_DETECT_ENABLE;
1389
1390         temp_word = (temp_word & ~intr_enable) | intr_enable; 
1391
1392         if (pciehp_poll_mode) {
1393                 temp_word = (temp_word & ~HP_INTR_ENABLE) | 0x0;
1394         } else {
1395                 temp_word = (temp_word & ~HP_INTR_ENABLE) | HP_INTR_ENABLE;
1396         }
1397
1398         /* Unmask Hot-plug Interrupt Enable for the interrupt notification mechanism case */
1399         rc = hp_register_write_word(pdev, SLOT_CTRL(ctrl->cap_base), temp_word);
1400         if (rc) {
1401                 err("%s : hp_register_write_word SLOT_CTRL failed\n", __FUNCTION__);
1402                 goto abort_free_ctlr;
1403         }
1404         rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), slot_status);
1405         if (rc) {
1406                 err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
1407                 goto abort_free_ctlr;
1408         }
1409         
1410         temp_word =  0x1F; /* Clear all events */
1411         rc = hp_register_write_word(php_ctlr->pci_dev, SLOT_STATUS(ctrl->cap_base), temp_word);
1412         if (rc) {
1413                 err("%s : hp_register_write_word SLOT_STATUS failed\n", __FUNCTION__);
1414                 goto abort_free_ctlr;
1415         }
1416         
1417         if (pciehp_force) {
1418                 dbg("Bypassing BIOS check for pciehp use on %s\n",
1419                                 pci_name(ctrl->pci_dev));
1420         } else {
1421                 rc = pciehp_get_hp_hw_control_from_firmware(ctrl->pci_dev);
1422                 if (rc)
1423                         goto abort_free_ctlr;
1424         }
1425
1426         /*  Add this HPC instance into the HPC list */
1427         spin_lock(&list_lock);
1428         if (php_ctlr_list_head == 0) {
1429                 php_ctlr_list_head = php_ctlr;
1430                 p = php_ctlr_list_head;
1431                 p->pnext = NULL;
1432         } else {
1433                 p = php_ctlr_list_head;
1434
1435                 while (p->pnext)
1436                         p = p->pnext;
1437
1438                 p->pnext = php_ctlr;
1439         }
1440         spin_unlock(&list_lock);
1441
1442         ctlr_seq_num++;
1443         ctrl->hpc_ctlr_handle = php_ctlr;
1444         ctrl->hpc_ops = &pciehp_hpc_ops;
1445
1446         DBG_LEAVE_ROUTINE
1447         return 0;
1448
1449         /* We end up here for the many possible ways to fail this API.  */
1450 abort_free_ctlr:
1451         pcie_cap_base = saved_cap_base;
1452         kfree(php_ctlr);
1453 abort:
1454         DBG_LEAVE_ROUTINE
1455         return -1;
1456 }