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