patch-2_6_7-vs1_9_1_12
[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 = 0; /* HPC state linked list */
241 static int ctlr_seq_num = 0;    /* 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         if ( (lnk_status & (LNK_TRN | LNK_TRN_ERR)) == 0x0C00) {
353                 err("%s : Link Training Error occurs \n", __FUNCTION__);
354                 retval = -1;
355                 return retval;
356         }
357
358         DBG_LEAVE_ROUTINE 
359         return retval;
360 }
361
362
363 static int hpc_get_attention_status(struct slot *slot, u8 *status)
364 {
365         struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
366         u16 slot_ctrl;
367         u8 atten_led_state;
368         int retval = 0;
369         
370         DBG_ENTER_ROUTINE 
371
372         if (!php_ctlr) {
373                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
374                 return -1;
375         }
376
377         retval = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL, slot_ctrl);
378
379         if (retval) {
380                 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
381                 return retval;
382         }
383
384         dbg("%s: SLOT_CTRL %x, value read %x\n", __FUNCTION__,SLOT_CTRL, slot_ctrl);
385
386         atten_led_state = (slot_ctrl & ATTN_LED_CTRL) >> 6;
387
388         switch (atten_led_state) {
389         case 0:
390                 *status = 0xFF; /* Reserved */
391                 break;
392         case 1:
393                 *status = 1;    /* On */
394                 break;
395         case 2:
396                 *status = 2;    /* Blink */
397                 break;
398         case 3:
399                 *status = 0;    /* Off */
400                 break;
401         default:
402                 *status = 0xFF;
403                 break;
404         }
405
406         DBG_LEAVE_ROUTINE 
407         return 0;
408 }
409
410 static int hpc_get_power_status(struct slot * slot, u8 *status)
411 {
412         struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
413         u16 slot_ctrl;
414         u8 pwr_state;
415         int     retval = 0;
416         
417         DBG_ENTER_ROUTINE 
418
419         if (!php_ctlr) {
420                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
421                 return -1;
422         }
423
424         retval = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL, slot_ctrl);
425
426         if (retval) {
427                 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
428                 return retval;
429         }
430         dbg("%s: SLOT_CTRL %x value read %x\n", __FUNCTION__, SLOT_CTRL, slot_ctrl);
431
432         pwr_state = (slot_ctrl & PWR_CTRL) >> 10;
433
434         switch (pwr_state) {
435         case 0:
436                 *status = 1;
437                 break;
438         case 1:
439                 *status = 0;    
440                 break;
441         default:
442                 *status = 0xFF;
443                 break;
444         }
445
446         DBG_LEAVE_ROUTINE 
447         return retval;
448 }
449
450
451 static int hpc_get_latch_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         int retval = 0;
456
457         DBG_ENTER_ROUTINE 
458
459         if (!php_ctlr) {
460                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
461                 return -1;
462         }
463
464         retval = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS, slot_status);
465
466         if (retval) {
467                 err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
468                 return retval;
469         }
470
471         *status = (((slot_status & MRL_STATE) >> 5) == 0) ? 0 : 1;  
472
473         DBG_LEAVE_ROUTINE 
474         return 0;
475 }
476
477 static int hpc_get_adapter_status(struct slot *slot, u8 *status)
478 {
479         struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
480         u16 slot_status;
481         u8 card_state;
482         int retval = 0;
483
484         DBG_ENTER_ROUTINE 
485
486         if (!php_ctlr) {
487                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
488                 return -1;
489         }
490
491         retval = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS, slot_status);
492
493         if (retval) {
494                 err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
495                 return retval;
496         }
497         card_state = (u8)((slot_status & PRSN_STATE) >> 6);
498         *status = (card_state == 1) ? 1 : 0;
499
500         DBG_LEAVE_ROUTINE 
501         return 0;
502 }
503
504 static int hpc_query_power_fault(struct slot * slot)
505 {
506         struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
507         u16 slot_status;
508         u8 pwr_fault;
509         int retval = 0;
510         u8 status;
511
512         DBG_ENTER_ROUTINE 
513
514         if (!php_ctlr) {
515                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
516                 return -1;
517         }
518
519         retval = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS, slot_status);
520
521         if (retval) {
522                 err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
523                 return retval;
524         }
525         pwr_fault = (u8)((slot_status & PWR_FAULT_DETECTED) >> 1);
526         status = (pwr_fault != 1) ? 1 : 0;
527         
528         DBG_LEAVE_ROUTINE
529         /* Note: Logic 0 => fault */
530         return status;
531 }
532
533 static int hpc_set_attention_status(struct slot *slot, u8 value)
534 {
535         struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
536         u16 slot_cmd = 0;
537         u16 slot_ctrl;
538         int rc = 0;
539
540         dbg("%s: \n", __FUNCTION__);
541         if (!php_ctlr) {
542                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
543                 return -1;
544         }
545
546         if (slot->hp_slot >= php_ctlr->num_slots) {
547                 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
548                 return -1;
549         }
550         rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL, slot_ctrl);
551
552         if (rc) {
553                 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
554                 return rc;
555         }
556         dbg("%s : hp_register_read_word SLOT_CTRL %x\n", __FUNCTION__, slot_ctrl);
557
558         switch (value) {
559                 case 0 :        /* turn off */
560                         slot_cmd = (slot_ctrl & ~ATTN_LED_CTRL) | 0x00C0;
561                         break;
562                 case 1:         /* turn on */
563                         slot_cmd = (slot_ctrl & ~ATTN_LED_CTRL) | 0x0040;
564                         break;
565                 case 2:         /* turn blink */
566                         slot_cmd = (slot_ctrl & ~ATTN_LED_CTRL) | 0x0080;
567                         break;
568                 default:
569                         return -1;
570         }
571         if (!pciehp_poll_mode)
572                 slot_cmd = slot_cmd | HP_INTR_ENABLE; 
573
574         pcie_write_cmd(slot, slot_cmd);
575         dbg("%s: SLOT_CTRL %x write cmd %x\n", __FUNCTION__, SLOT_CTRL, slot_cmd);
576         
577         return rc;
578 }
579
580
581 static void hpc_set_green_led_on(struct slot *slot)
582 {
583         struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
584         u16 slot_cmd;
585         u16 slot_ctrl;
586         int rc = 0;
587         
588         dbg("%s: \n", __FUNCTION__);    
589         if (!php_ctlr) {
590                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
591                 return ;
592         }
593
594         if (slot->hp_slot >= php_ctlr->num_slots) {
595                 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
596                 return ;
597         }
598
599         rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL, slot_ctrl);
600
601         if (rc) {
602                 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
603                 return;
604         }
605         dbg("%s : hp_register_read_word SLOT_CTRL %x\n", __FUNCTION__, slot_ctrl);
606         slot_cmd = (slot_ctrl & ~PWR_LED_CTRL) | 0x0100;
607         if (!pciehp_poll_mode)
608                 slot_cmd = slot_cmd | HP_INTR_ENABLE; 
609
610         pcie_write_cmd(slot, slot_cmd);
611
612         dbg("%s: SLOT_CTRL %x write cmd %x\n",__FUNCTION__, SLOT_CTRL, slot_cmd);
613         return;
614 }
615
616 static void hpc_set_green_led_off(struct slot *slot)
617 {
618         struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
619         u16 slot_cmd;
620         u16 slot_ctrl;
621         int rc = 0;
622
623         dbg("%s: \n", __FUNCTION__);    
624         if (!php_ctlr) {
625                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
626                 return ;
627         }
628
629         if (slot->hp_slot >= php_ctlr->num_slots) {
630                 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
631                 return ;
632         }
633
634         rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL, slot_ctrl);
635
636         if (rc) {
637                 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
638                 return;
639         }
640         dbg("%s : hp_register_read_word SLOT_CTRL %x\n", __FUNCTION__, slot_ctrl);
641
642         slot_cmd = (slot_ctrl & ~PWR_LED_CTRL) | 0x0300;
643
644         if (!pciehp_poll_mode)
645                 slot_cmd = slot_cmd | HP_INTR_ENABLE; 
646         pcie_write_cmd(slot, slot_cmd);
647         dbg("%s: SLOT_CTRL %x write cmd %x\n", __FUNCTION__, SLOT_CTRL, slot_cmd);
648
649         return;
650 }
651
652 static void hpc_set_green_led_blink(struct slot *slot)
653 {
654         struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
655         u16 slot_cmd;
656         u16 slot_ctrl;
657         int rc = 0; 
658         
659         dbg("%s: \n", __FUNCTION__);    
660         if (!php_ctlr) {
661                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
662                 return ;
663         }
664
665         if (slot->hp_slot >= php_ctlr->num_slots) {
666                 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
667                 return ;
668         }
669
670         rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL, slot_ctrl);
671
672         if (rc) {
673                 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
674                 return;
675         }
676         dbg("%s : hp_register_read_word SLOT_CTRL %x\n", __FUNCTION__, slot_ctrl);
677
678         slot_cmd = (slot_ctrl & ~PWR_LED_CTRL) | 0x0200;
679
680         if (!pciehp_poll_mode)
681                 slot_cmd = slot_cmd | HP_INTR_ENABLE; 
682         pcie_write_cmd(slot, slot_cmd);
683
684         dbg("%s: SLOT_CTRL %x write cmd %x\n",__FUNCTION__, SLOT_CTRL, slot_cmd);
685         return;
686 }
687
688 int pcie_get_ctlr_slot_config(struct controller *ctrl,
689         int *num_ctlr_slots,    /* number of slots in this HPC; only 1 in PCIE  */      
690         int *first_device_num,  /* PCI dev num of the first slot in this PCIE   */
691         int *physical_slot_num, /* phy slot num of the first slot in this PCIE  */
692         int *updown,            /* physical_slot_num increament: 1 or -1        */
693         int *flags)
694 {
695         struct php_ctlr_state_s *php_ctlr = ctrl->hpc_ctlr_handle;
696         u32 slot_cap;
697         int rc = 0;
698         
699         DBG_ENTER_ROUTINE 
700
701         if (!php_ctlr) {
702                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
703                 return -1;
704         }
705
706         *first_device_num = 0;
707         *num_ctlr_slots = 1; 
708
709         rc = hp_register_read_dword(php_ctlr->pci_dev, SLOT_CAP, slot_cap);
710
711         if (rc) {
712                 err("%s : hp_register_read_dword SLOT_CAP failed\n", __FUNCTION__);
713                 return -1;
714         }
715         
716         *physical_slot_num = slot_cap >> 19;
717
718         *updown = -1;
719
720         DBG_LEAVE_ROUTINE 
721         return 0;
722 }
723
724 static void hpc_release_ctlr(struct controller *ctrl)
725 {
726         struct php_ctlr_state_s *php_ctlr = ctrl->hpc_ctlr_handle;
727         struct php_ctlr_state_s *p, *p_prev;
728
729         DBG_ENTER_ROUTINE 
730
731         if (!php_ctlr) {
732                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
733                 return ;
734         }
735
736         if (pciehp_poll_mode) {
737             del_timer(&php_ctlr->int_poll_timer);
738         } else {        
739                 if (php_ctlr->irq) {
740                         free_irq(php_ctlr->irq, ctrl);
741                         php_ctlr->irq = 0;
742                 }
743         }
744         if (php_ctlr->pci_dev) 
745                 php_ctlr->pci_dev = 0;
746
747         spin_lock(&list_lock);
748         p = php_ctlr_list_head;
749         p_prev = NULL;
750         while (p) {
751                 if (p == php_ctlr) {
752                         if (p_prev)
753                                 p_prev->pnext = p->pnext;
754                         else
755                                 php_ctlr_list_head = p->pnext;
756                         break;
757                 } else {
758                         p_prev = p;
759                         p = p->pnext;
760                 }
761         }
762         spin_unlock(&list_lock);
763
764         kfree(php_ctlr);
765
766         DBG_LEAVE_ROUTINE
767                           
768 }
769
770 static int hpc_power_on_slot(struct slot * slot)
771 {
772         struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
773         u16 slot_cmd;
774         u16 slot_ctrl;
775
776         int retval = 0;
777
778         DBG_ENTER_ROUTINE 
779         dbg("%s: \n", __FUNCTION__);    
780
781         if (!php_ctlr) {
782                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
783                 return -1;
784         }
785
786         dbg("%s: slot->hp_slot %x\n", __FUNCTION__, slot->hp_slot);
787         if (slot->hp_slot >= php_ctlr->num_slots) {
788                 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
789                 return -1;
790         }
791
792         retval = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL, slot_ctrl);
793
794         if (retval) {
795                 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
796                 return retval;
797         }
798         dbg("%s: SLOT_CTRL %x, value read %xn", __FUNCTION__, SLOT_CTRL, 
799                 slot_ctrl);
800
801         slot_cmd = (slot_ctrl & ~PWR_CTRL) | POWER_ON;
802
803         if (!pciehp_poll_mode)
804                 slot_cmd = slot_cmd | HP_INTR_ENABLE; 
805
806         retval = pcie_write_cmd(slot, slot_cmd);
807
808         if (retval) {
809                 err("%s: Write %x command failed!\n", __FUNCTION__, slot_cmd);
810                 return -1;
811         }
812         dbg("%s: SLOT_CTRL %x write cmd %x\n",__FUNCTION__, SLOT_CTRL, slot_cmd);
813
814         DBG_LEAVE_ROUTINE
815
816         return retval;
817 }
818
819 static int hpc_power_off_slot(struct slot * slot)
820 {
821         struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
822         u16 slot_cmd;
823         u16 slot_ctrl;
824
825         int retval = 0;
826
827         DBG_ENTER_ROUTINE 
828         dbg("%s: \n", __FUNCTION__);    
829
830         if (!php_ctlr) {
831                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
832                 return -1;
833         }
834
835         dbg("%s: slot->hp_slot %x\n", __FUNCTION__, slot->hp_slot);
836         slot->hp_slot = 0;
837         if (slot->hp_slot >= php_ctlr->num_slots) {
838                 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
839                 return -1;
840         }
841         retval = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL, slot_ctrl);
842
843         if (retval) {
844                 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
845                 return retval;
846         }
847         dbg("%s: SLOT_CTRL %x, value read %x\n", __FUNCTION__, SLOT_CTRL, 
848                 slot_ctrl);
849
850         slot_cmd = (slot_ctrl & ~PWR_CTRL) | POWER_OFF;
851
852         if (!pciehp_poll_mode)
853                 slot_cmd = slot_cmd | HP_INTR_ENABLE; 
854
855         retval = pcie_write_cmd(slot, slot_cmd);
856
857         if (retval) {
858                 err("%s: Write command failed!\n", __FUNCTION__);
859                 return -1;
860         }
861         dbg("%s: SLOT_CTRL %x write cmd %x\n",__FUNCTION__, SLOT_CTRL, slot_cmd);
862
863         DBG_LEAVE_ROUTINE
864
865         return retval;
866 }
867
868 static irqreturn_t pcie_isr(int IRQ, void *dev_id, struct pt_regs *regs)
869 {
870         struct controller *ctrl = NULL;
871         struct php_ctlr_state_s *php_ctlr;
872         u8 schedule_flag = 0;
873         u16 slot_status, intr_detect, intr_loc;
874         u16 temp_word;
875         int hp_slot = 0;        /* only 1 slot per PCI Express port */
876         int rc = 0;
877
878         if (!dev_id)
879                 return IRQ_NONE;
880
881         if (!pciehp_poll_mode) { 
882                 ctrl = dev_id;
883                 php_ctlr = ctrl->hpc_ctlr_handle;
884         } else {
885                 php_ctlr = dev_id;
886                 ctrl = (struct controller *)php_ctlr->callback_instance_id;
887         }
888
889         if (!ctrl) {
890                 dbg("%s: dev_id %p ctlr == NULL\n", __FUNCTION__, (void*) dev_id);
891                 return IRQ_NONE;
892         }
893         
894         if (!php_ctlr) {
895                 dbg("%s: php_ctlr == NULL\n", __FUNCTION__);
896                 return IRQ_NONE;
897         }
898
899         rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS, slot_status);
900         if (rc) {
901                 err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
902                 return IRQ_NONE;
903         }
904
905         intr_detect = ( ATTN_BUTTN_PRESSED | PWR_FAULT_DETECTED | MRL_SENS_CHANGED |
906                                         PRSN_DETECT_CHANGED | CMD_COMPLETED );
907
908         intr_loc = slot_status & intr_detect;
909
910         /* Check to see if it was our interrupt */
911         if ( !intr_loc )
912                 return IRQ_NONE;
913
914         dbg("%s: intr_loc %x\n", __FUNCTION__, intr_loc);
915         /* Mask Hot-plug Interrupt Enable */
916         if (!pciehp_poll_mode) {
917                 rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL, temp_word);
918                 if (rc) {
919                         err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
920                         return IRQ_NONE;
921                 }
922
923                 dbg("%s: Set Mask Hot-plug Interrupt Enable\n", __FUNCTION__);
924                 dbg("%s: hp_register_read_word SLOT_CTRL with value %x\n", __FUNCTION__, temp_word);
925                 temp_word = (temp_word & ~HP_INTR_ENABLE & ~CMD_CMPL_INTR_ENABLE) | 0x00;
926
927                 rc = hp_register_write_word(php_ctlr->pci_dev, SLOT_CTRL, temp_word);
928                 if (rc) {
929                         err("%s : hp_register_write_word SLOT_CTRL failed\n", __FUNCTION__);
930                         return IRQ_NONE;
931                 }
932                 dbg("%s: hp_register_write_word SLOT_CTRL with value %x\n", __FUNCTION__, temp_word);
933                 
934                 rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS, slot_status);
935                 if (rc) {
936                         err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
937                         return IRQ_NONE;
938                 }
939                 dbg("%s: hp_register_read_word SLOT_STATUS with value %x\n", __FUNCTION__, slot_status); 
940                 
941                 /* Clear command complete interrupt caused by this write */
942                 temp_word = 0x1f;
943                 rc = hp_register_write_word(php_ctlr->pci_dev, SLOT_STATUS, temp_word);
944                 if (rc) {
945                         err("%s : hp_register_write_word SLOT_STATUS failed\n", __FUNCTION__);
946                         return IRQ_NONE;
947                 }
948                 dbg("%s: hp_register_write_word SLOT_STATUS with value %x\n", __FUNCTION__, temp_word);
949         }
950         
951         if (intr_loc & CMD_COMPLETED) {
952                 /* 
953                  * Command Complete Interrupt Pending 
954                  */
955                 dbg("%s: In Command Complete Interrupt Pending\n", __FUNCTION__);
956                 wake_up_interruptible(&ctrl->queue);
957         }
958
959         if ((php_ctlr->switch_change_callback) && (intr_loc & MRL_SENS_CHANGED))
960                 schedule_flag += php_ctlr->switch_change_callback(
961                         hp_slot, php_ctlr->callback_instance_id);
962         if ((php_ctlr->attention_button_callback) && (intr_loc & ATTN_BUTTN_PRESSED))
963                 schedule_flag += php_ctlr->attention_button_callback(
964                         hp_slot, php_ctlr->callback_instance_id);
965         if ((php_ctlr->presence_change_callback) && (intr_loc & PRSN_DETECT_CHANGED))
966                 schedule_flag += php_ctlr->presence_change_callback(
967                         hp_slot , php_ctlr->callback_instance_id);
968         if ((php_ctlr->power_fault_callback) && (intr_loc & PWR_FAULT_DETECTED))
969                 schedule_flag += php_ctlr->power_fault_callback(
970                         hp_slot, php_ctlr->callback_instance_id);
971
972         /* Clear all events after serving them */
973         temp_word = 0x1F;
974         rc = hp_register_write_word(php_ctlr->pci_dev, SLOT_STATUS, temp_word);
975         if (rc) {
976                 err("%s : hp_register_write_word SLOT_STATUS failed\n", __FUNCTION__);
977                 return IRQ_NONE;
978         }
979         /* Unmask Hot-plug Interrupt Enable */
980         if (!pciehp_poll_mode) {
981                 rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL, temp_word);
982                 if (rc) {
983                         err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
984                         return IRQ_NONE;
985                 }
986
987                 dbg("%s: Unmask Hot-plug Interrupt Enable\n", __FUNCTION__);
988                 dbg("%s: hp_register_read_word SLOT_CTRL with value %x\n", __FUNCTION__, temp_word);
989                 temp_word = (temp_word & ~HP_INTR_ENABLE) | HP_INTR_ENABLE;
990
991                 rc = hp_register_write_word(php_ctlr->pci_dev, SLOT_CTRL, temp_word);
992                 if (rc) {
993                         err("%s : hp_register_write_word SLOT_CTRL failed\n", __FUNCTION__);
994                         return IRQ_NONE;
995                 }
996                 dbg("%s: hp_register_write_word SLOT_CTRL with value %x\n", __FUNCTION__, temp_word);   
997         
998                 rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS, slot_status);
999                 if (rc) {
1000                         err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
1001                         return IRQ_NONE;
1002                 }
1003                 dbg("%s: hp_register_read_word SLOT_STATUS with value %x\n", __FUNCTION__, slot_status); 
1004                 
1005                 /* Clear command complete interrupt caused by this write */
1006                 temp_word = 0x1F;
1007                 rc = hp_register_write_word(php_ctlr->pci_dev, SLOT_STATUS, temp_word);
1008                 if (rc) {
1009                         err("%s : hp_register_write_word SLOT_STATUS failed\n", __FUNCTION__);
1010                         return IRQ_NONE;
1011                 }
1012                 dbg("%s: hp_register_write_word SLOT_STATUS with value %x\n", __FUNCTION__, temp_word); 
1013         }
1014         
1015         return IRQ_HANDLED;
1016 }
1017
1018 static int hpc_get_max_lnk_speed (struct slot *slot, enum pcie_link_speed *value)
1019 {
1020         struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
1021         enum pcie_link_speed lnk_speed;
1022         u32     lnk_cap;
1023         int retval = 0;
1024
1025         DBG_ENTER_ROUTINE 
1026
1027         if (!php_ctlr) {
1028                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
1029                 return -1;
1030         }
1031
1032         if (slot->hp_slot >= php_ctlr->num_slots) {
1033                 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
1034                 return -1;
1035         }
1036
1037         retval = hp_register_read_dword(php_ctlr->pci_dev, LNK_CAP, lnk_cap);
1038
1039         if (retval) {
1040                 err("%s : hp_register_read_dword  LNK_CAP failed\n", __FUNCTION__);
1041                 return retval;
1042         }
1043
1044         switch (lnk_cap & 0x000F) {
1045         case 1:
1046                 lnk_speed = PCIE_2PT5GB;
1047                 break;
1048         default:
1049                 lnk_speed = PCIE_LNK_SPEED_UNKNOWN;
1050                 break;
1051         }
1052
1053         *value = lnk_speed;
1054         dbg("Max link speed = %d\n", lnk_speed);
1055         DBG_LEAVE_ROUTINE 
1056         return retval;
1057 }
1058
1059 static int hpc_get_max_lnk_width (struct slot *slot, enum pcie_link_width *value)
1060 {
1061         struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
1062         enum pcie_link_width lnk_wdth;
1063         u32     lnk_cap;
1064         int retval = 0;
1065
1066         DBG_ENTER_ROUTINE 
1067
1068         if (!php_ctlr) {
1069                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
1070                 return -1;
1071         }
1072
1073         if (slot->hp_slot >= php_ctlr->num_slots) {
1074                 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
1075                 return -1;
1076         }
1077
1078         retval = hp_register_read_dword(php_ctlr->pci_dev, LNK_CAP, lnk_cap);
1079
1080         if (retval) {
1081                 err("%s : hp_register_read_dword  LNK_CAP failed\n", __FUNCTION__);
1082                 return retval;
1083         }
1084
1085         switch ((lnk_cap & 0x03F0) >> 4){
1086         case 0:
1087                 lnk_wdth = PCIE_LNK_WIDTH_RESRV;
1088                 break;
1089         case 1:
1090                 lnk_wdth = PCIE_LNK_X1;
1091                 break;
1092         case 2:
1093                 lnk_wdth = PCIE_LNK_X2;
1094                 break;
1095         case 4:
1096                 lnk_wdth = PCIE_LNK_X4;
1097                 break;
1098         case 8:
1099                 lnk_wdth = PCIE_LNK_X8;
1100                 break;
1101         case 12:
1102                 lnk_wdth = PCIE_LNK_X12;
1103                 break;
1104         case 16:
1105                 lnk_wdth = PCIE_LNK_X16;
1106                 break;
1107         case 32:
1108                 lnk_wdth = PCIE_LNK_X32;
1109                 break;
1110         default:
1111                 lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
1112                 break;
1113         }
1114
1115         *value = lnk_wdth;
1116         dbg("Max link width = %d\n", lnk_wdth);
1117         DBG_LEAVE_ROUTINE 
1118         return retval;
1119 }
1120
1121 static int hpc_get_cur_lnk_speed (struct slot *slot, enum pcie_link_speed *value)
1122 {
1123         struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
1124         enum pcie_link_speed lnk_speed = PCI_SPEED_UNKNOWN;
1125         int retval = 0;
1126         u16 lnk_status;
1127
1128         DBG_ENTER_ROUTINE 
1129
1130         if (!php_ctlr) {
1131                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
1132                 return -1;
1133         }
1134
1135         if (slot->hp_slot >= php_ctlr->num_slots) {
1136                 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
1137                 return -1;
1138         }
1139
1140         retval = hp_register_read_word(php_ctlr->pci_dev, LNK_STATUS, lnk_status);
1141
1142         if (retval) {
1143                 err("%s : hp_register_read_word LNK_STATUS failed\n", __FUNCTION__);
1144                 return retval;
1145         }
1146
1147         switch (lnk_status & 0x0F) {
1148         case 1:
1149                 lnk_speed = PCIE_2PT5GB;
1150                 break;
1151         default:
1152                 lnk_speed = PCIE_LNK_SPEED_UNKNOWN;
1153                 break;
1154         }
1155
1156         *value = lnk_speed;
1157         dbg("Current link speed = %d\n", lnk_speed);
1158         DBG_LEAVE_ROUTINE 
1159         return retval;
1160 }
1161
1162 static int hpc_get_cur_lnk_width (struct slot *slot, enum pcie_link_width *value)
1163 {
1164         struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
1165         enum pcie_link_width lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
1166         int retval = 0;
1167         u16 lnk_status;
1168
1169         DBG_ENTER_ROUTINE 
1170
1171         if (!php_ctlr) {
1172                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
1173                 return -1;
1174         }
1175
1176         if (slot->hp_slot >= php_ctlr->num_slots) {
1177                 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
1178                 return -1;
1179         }
1180
1181         retval = hp_register_read_word(php_ctlr->pci_dev, LNK_STATUS, lnk_status);
1182
1183         if (retval) {
1184                 err("%s : hp_register_read_word LNK_STATUS failed\n", __FUNCTION__);
1185                 return retval;
1186         }
1187         
1188         switch ((lnk_status & 0x03F0) >> 4){
1189         case 0:
1190                 lnk_wdth = PCIE_LNK_WIDTH_RESRV;
1191                 break;
1192         case 1:
1193                 lnk_wdth = PCIE_LNK_X1;
1194                 break;
1195         case 2:
1196                 lnk_wdth = PCIE_LNK_X2;
1197                 break;
1198         case 4:
1199                 lnk_wdth = PCIE_LNK_X4;
1200                 break;
1201         case 8:
1202                 lnk_wdth = PCIE_LNK_X8;
1203                 break;
1204         case 12:
1205                 lnk_wdth = PCIE_LNK_X12;
1206                 break;
1207         case 16:
1208                 lnk_wdth = PCIE_LNK_X16;
1209                 break;
1210         case 32:
1211                 lnk_wdth = PCIE_LNK_X32;
1212                 break;
1213         default:
1214                 lnk_wdth = PCIE_LNK_WIDTH_UNKNOWN;
1215                 break;
1216         }
1217
1218         *value = lnk_wdth;
1219         dbg("Current link width = %d\n", lnk_wdth);
1220         DBG_LEAVE_ROUTINE 
1221         return retval;
1222 }
1223
1224 static struct hpc_ops pciehp_hpc_ops = {
1225         .power_on_slot                  = hpc_power_on_slot,
1226         .power_off_slot                 = hpc_power_off_slot,
1227         .set_attention_status           = hpc_set_attention_status,
1228         .get_power_status               = hpc_get_power_status,
1229         .get_attention_status           = hpc_get_attention_status,
1230         .get_latch_status               = hpc_get_latch_status,
1231         .get_adapter_status             = hpc_get_adapter_status,
1232
1233         .get_max_bus_speed              = hpc_get_max_lnk_speed,
1234         .get_cur_bus_speed              = hpc_get_cur_lnk_speed,
1235         .get_max_lnk_width              = hpc_get_max_lnk_width,
1236         .get_cur_lnk_width              = hpc_get_cur_lnk_width,
1237         
1238         .query_power_fault              = hpc_query_power_fault,
1239         .green_led_on                   = hpc_set_green_led_on,
1240         .green_led_off                  = hpc_set_green_led_off,
1241         .green_led_blink                = hpc_set_green_led_blink,
1242         
1243         .release_ctlr                   = hpc_release_ctlr,
1244         .check_lnk_status               = hpc_check_lnk_status,
1245 };
1246
1247 int pcie_init(struct controller * ctrl,
1248         struct pci_dev *pdev,
1249         php_intr_callback_t attention_button_callback,
1250         php_intr_callback_t switch_change_callback,
1251         php_intr_callback_t presence_change_callback,
1252         php_intr_callback_t power_fault_callback)
1253 {
1254         struct php_ctlr_state_s *php_ctlr, *p;
1255         void *instance_id = ctrl;
1256         int rc;
1257         static int first = 1;
1258         u16 temp_word;
1259         u16 cap_reg;
1260         u16 intr_enable;
1261         u32 slot_cap;
1262         int cap_base, saved_cap_base;
1263         u16 slot_status, slot_ctrl;
1264
1265         DBG_ENTER_ROUTINE
1266         
1267         spin_lock_init(&list_lock);     
1268         php_ctlr = (struct php_ctlr_state_s *) kmalloc(sizeof(struct php_ctlr_state_s), GFP_KERNEL);
1269
1270         if (!php_ctlr) {        /* allocate controller state data */
1271                 err("%s: HPC controller memory allocation error!\n", __FUNCTION__);
1272                 goto abort;
1273         }
1274
1275         memset(php_ctlr, 0, sizeof(struct php_ctlr_state_s));
1276
1277         php_ctlr->pci_dev = pdev;       /* save pci_dev in context */
1278
1279         dbg("%s: pdev->vendor %x pdev->device %x\n", __FUNCTION__,
1280                 pdev->vendor, pdev->device);
1281
1282         saved_cap_base = pcie_cap_base;
1283
1284         if ((cap_base = pci_find_capability(pdev, PCI_CAP_ID_EXP)) == 0) {
1285                 dbg("%s: Can't find PCI_CAP_ID_EXP (0x10)\n", __FUNCTION__);
1286                 goto abort_free_ctlr;
1287         }
1288
1289         pcie_cap_base = cap_base;
1290
1291         dbg("%s: pcie_cap_base %x\n", __FUNCTION__, pcie_cap_base);
1292
1293         rc = hp_register_read_word(pdev, CAP_REG, cap_reg);
1294         if (rc) {
1295                 err("%s : hp_register_read_word CAP_REG failed\n", __FUNCTION__);
1296                 goto abort_free_ctlr;
1297         }
1298         dbg("%s: CAP_REG offset %x cap_reg %x\n", __FUNCTION__, CAP_REG, cap_reg);
1299
1300         if (((cap_reg & SLOT_IMPL) == 0) || ((cap_reg & DEV_PORT_TYPE) != 0x0040)){
1301                 dbg("%s : This is not a root port or the port is not connected to a slot\n", __FUNCTION__);
1302                 goto abort_free_ctlr;
1303         }
1304
1305         rc = hp_register_read_dword(php_ctlr->pci_dev, SLOT_CAP, slot_cap);
1306         if (rc) {
1307                 err("%s : hp_register_read_word CAP_REG failed\n", __FUNCTION__);
1308                 goto abort_free_ctlr;
1309         }
1310         dbg("%s: SLOT_CAP offset %x slot_cap %x\n", __FUNCTION__, SLOT_CAP, slot_cap);
1311
1312         if (!(slot_cap & HP_CAP)) {
1313                 dbg("%s : This slot is not hot-plug capable\n", __FUNCTION__);
1314                 goto abort_free_ctlr;
1315         }
1316         /* For debugging purpose */
1317         rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS, slot_status);
1318         if (rc) {
1319                 err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
1320                 goto abort_free_ctlr;
1321         }
1322         dbg("%s: SLOT_STATUS offset %x slot_status %x\n", __FUNCTION__, SLOT_STATUS, slot_status);
1323
1324         rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_CTRL, slot_ctrl);
1325         if (rc) {
1326                 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
1327                 goto abort_free_ctlr;
1328         }
1329         dbg("%s: SLOT_CTRL offset %x slot_ctrl %x\n", __FUNCTION__, SLOT_CTRL, slot_ctrl);
1330
1331         if (first) {
1332                 spin_lock_init(&hpc_event_lock);
1333                 first = 0;
1334         }
1335
1336         dbg("pdev = %p: b:d:f:irq=0x%x:%x:%x:%x\n", pdev, pdev->bus->number, 
1337                 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn), pdev->irq);
1338         for ( rc = 0; rc < DEVICE_COUNT_RESOURCE; rc++)
1339                 if (pci_resource_len(pdev, rc) > 0)
1340                         dbg("pci resource[%d] start=0x%lx(len=0x%lx)\n", rc,
1341                                 pci_resource_start(pdev, rc), pci_resource_len(pdev, rc));
1342
1343         info("HPC vendor_id %x device_id %x ss_vid %x ss_did %x\n", pdev->vendor, pdev->device, 
1344                 pdev->subsystem_vendor, pdev->subsystem_device);
1345
1346         init_MUTEX(&ctrl->crit_sect);
1347         /* setup wait queue */
1348         init_waitqueue_head(&ctrl->queue);
1349
1350         /* find the IRQ */
1351         php_ctlr->irq = pdev->irq;
1352         dbg("HPC interrupt = %d\n", php_ctlr->irq);
1353
1354         /* Save interrupt callback info */
1355         php_ctlr->attention_button_callback = attention_button_callback;
1356         php_ctlr->switch_change_callback = switch_change_callback;
1357         php_ctlr->presence_change_callback = presence_change_callback;
1358         php_ctlr->power_fault_callback = power_fault_callback;
1359         php_ctlr->callback_instance_id = instance_id;
1360
1361         /* return PCI Controller Info */
1362         php_ctlr->slot_device_offset = 0;
1363         php_ctlr->num_slots = 1;
1364
1365         /* Mask Hot-plug Interrupt Enable */
1366         rc = hp_register_read_word(pdev, SLOT_CTRL, temp_word);
1367         if (rc) {
1368                 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
1369                 goto abort_free_ctlr;
1370         }
1371
1372         dbg("%s: SLOT_CTRL %x value read %x\n", __FUNCTION__, SLOT_CTRL, temp_word);
1373         temp_word = (temp_word & ~HP_INTR_ENABLE & ~CMD_CMPL_INTR_ENABLE) | 0x00;
1374
1375         rc = hp_register_write_word(pdev, SLOT_CTRL, temp_word);
1376         if (rc) {
1377                 err("%s : hp_register_write_word SLOT_CTRL failed\n", __FUNCTION__);
1378                 goto abort_free_ctlr;
1379         }
1380         dbg("%s : Mask HPIE hp_register_write_word SLOT_CTRL %x\n", __FUNCTION__, temp_word);
1381
1382         rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS, slot_status);
1383         if (rc) {
1384                 err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
1385                 goto abort_free_ctlr;
1386         }
1387         dbg("%s: Mask HPIE SLOT_STATUS offset %x reads slot_status %x\n", __FUNCTION__, SLOT_STATUS, slot_status);
1388
1389         temp_word = 0x1F; /* Clear all events */
1390         rc = hp_register_write_word(php_ctlr->pci_dev, SLOT_STATUS, temp_word);
1391         if (rc) {
1392                 err("%s : hp_register_write_word SLOT_STATUS failed\n", __FUNCTION__);
1393                 goto abort_free_ctlr;
1394         }
1395         dbg("%s: SLOT_STATUS offset %x writes slot_status %x\n", __FUNCTION__, SLOT_STATUS, temp_word);
1396
1397         if (pciehp_poll_mode)  {/* Install interrupt polling code */
1398                 /* Install and start the interrupt polling timer */
1399                 init_timer(&php_ctlr->int_poll_timer);
1400                 start_int_poll_timer( php_ctlr, 10 );   /* start with 10 second delay */
1401         } else {
1402                 /* Installs the interrupt handler */
1403                 dbg("%s: pciehp_msi_quirk = %x\n", __FUNCTION__, pciehp_msi_quirk);
1404                 if (!pciehp_msi_quirk) {
1405                         rc = pci_enable_msi(pdev);
1406                         if (rc) {
1407                                 info("Can't get msi for the hotplug controller\n");
1408                                 info("Use INTx for the hotplug controller\n");
1409                                 dbg("%s: rc = %x\n", __FUNCTION__, rc);
1410                         } else 
1411                                 php_ctlr->irq = pdev->irq;
1412                 }
1413                 rc = request_irq(php_ctlr->irq, pcie_isr, SA_SHIRQ, MY_NAME, (void *) ctrl);
1414                 dbg("%s: request_irq %d for hpc%d (returns %d)\n", __FUNCTION__, php_ctlr->irq, ctlr_seq_num, rc);
1415                 if (rc) {
1416                         err("Can't get irq %d for the hotplug controller\n", php_ctlr->irq);
1417                         goto abort_free_ctlr;
1418                 }
1419         }
1420
1421         rc = hp_register_read_word(pdev, SLOT_CTRL, temp_word);
1422         if (rc) {
1423                 err("%s : hp_register_read_word SLOT_CTRL failed\n", __FUNCTION__);
1424                 goto abort_free_ctlr;
1425         }
1426         dbg("%s: SLOT_CTRL %x value read %x\n", __FUNCTION__, SLOT_CTRL, temp_word);
1427
1428         intr_enable = ATTN_BUTTN_ENABLE | PWR_FAULT_DETECT_ENABLE | MRL_DETECT_ENABLE |
1429                                         PRSN_DETECT_ENABLE;
1430
1431         temp_word = (temp_word & ~intr_enable) | intr_enable; 
1432
1433         if (pciehp_poll_mode) {
1434                 temp_word = (temp_word & ~HP_INTR_ENABLE) | 0x0;
1435         } else {
1436                 temp_word = (temp_word & ~HP_INTR_ENABLE) | HP_INTR_ENABLE;
1437         }
1438         dbg("%s: temp_word %x\n", __FUNCTION__, temp_word);
1439
1440         /* Unmask Hot-plug Interrupt Enable for the interrupt notification mechanism case */
1441         rc = hp_register_write_word(pdev, SLOT_CTRL, temp_word);
1442         if (rc) {
1443                 err("%s : hp_register_write_word SLOT_CTRL failed\n", __FUNCTION__);
1444                 goto abort_free_ctlr;
1445         }
1446         dbg("%s : Unmask HPIE hp_register_write_word SLOT_CTRL with %x\n", __FUNCTION__, temp_word);
1447         rc = hp_register_read_word(php_ctlr->pci_dev, SLOT_STATUS, slot_status);
1448         if (rc) {
1449                 err("%s : hp_register_read_word SLOT_STATUS failed\n", __FUNCTION__);
1450                 goto abort_free_ctlr;
1451         }
1452         dbg("%s: Unmask HPIE SLOT_STATUS offset %x reads slot_status %x\n", __FUNCTION__, 
1453                 SLOT_STATUS, slot_status);
1454         
1455         temp_word =  0x1F; /* Clear all events */
1456         rc = hp_register_write_word(php_ctlr->pci_dev, SLOT_STATUS, temp_word);
1457         if (rc) {
1458                 err("%s : hp_register_write_word SLOT_STATUS failed\n", __FUNCTION__);
1459                 goto abort_free_ctlr;
1460         }
1461         dbg("%s: SLOT_STATUS offset %x writes slot_status %x\n", __FUNCTION__, SLOT_STATUS, temp_word);
1462         
1463         /*  Add this HPC instance into the HPC list */
1464         spin_lock(&list_lock);
1465         if (php_ctlr_list_head == 0) {
1466                 php_ctlr_list_head = php_ctlr;
1467                 p = php_ctlr_list_head;
1468                 p->pnext = 0;
1469         } else {
1470                 p = php_ctlr_list_head;
1471
1472                 while (p->pnext)
1473                         p = p->pnext;
1474
1475                 p->pnext = php_ctlr;
1476         }
1477         spin_unlock(&list_lock);
1478
1479         ctlr_seq_num++;
1480         ctrl->hpc_ctlr_handle = php_ctlr;
1481         ctrl->hpc_ops = &pciehp_hpc_ops;
1482
1483         DBG_LEAVE_ROUTINE
1484         return 0;
1485
1486         /* We end up here for the many possible ways to fail this API.  */
1487 abort_free_ctlr:
1488         pcie_cap_base = saved_cap_base;
1489         kfree(php_ctlr);
1490 abort:
1491         DBG_LEAVE_ROUTINE
1492         return -1;
1493 }