2 * Standard Hot Plug Controller Driver
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
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.
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
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.
26 * Send feedback to <greg@kroah.com>, <kristen.c.accardi@intel.com>
30 #include <linux/config.h>
31 #include <linux/module.h>
32 #include <linux/kernel.h>
33 #include <linux/types.h>
34 #include <linux/slab.h>
35 #include <linux/workqueue.h>
36 #include <linux/interrupt.h>
37 #include <linux/delay.h>
38 #include <linux/wait.h>
39 #include <linux/smp_lock.h>
40 #include <linux/pci.h>
44 static void interrupt_event_handler(struct controller
*ctrl
);
46 static struct semaphore event_semaphore
; /* mutex for process loop (up if something to process) */
47 static struct semaphore event_exit
; /* guard ensure thread has exited before calling it quits */
48 static int event_finished
;
49 static unsigned long pushbutton_pending
; /* = 0 */
51 u8
shpchp_handle_attention_button(u8 hp_slot
, void *inst_id
)
53 struct controller
*ctrl
= (struct controller
*) inst_id
;
57 struct pci_func
*func
;
58 struct event_info
*taskInfo
;
60 /* Attention Button Change */
61 dbg("shpchp: Attention button interrupt received.\n");
63 func
= shpchp_slot_find(ctrl
->slot_bus
, (hp_slot
+ ctrl
->slot_device_offset
), 0);
65 /* This is the structure that tells the worker thread what to do */
66 taskInfo
= &(ctrl
->event_queue
[ctrl
->next_event
]);
67 p_slot
= shpchp_find_slot(ctrl
, hp_slot
+ ctrl
->slot_device_offset
);
69 p_slot
->hpc_ops
->get_adapter_status(p_slot
, &(func
->presence_save
));
70 p_slot
->hpc_ops
->get_latch_status(p_slot
, &getstatus
);
72 ctrl
->next_event
= (ctrl
->next_event
+ 1) % 10;
73 taskInfo
->hp_slot
= hp_slot
;
78 * Button pressed - See if need to TAKE ACTION!!!
80 info("Button pressed on Slot(%d)\n", ctrl
->first_slot
+ hp_slot
);
81 taskInfo
->event_type
= INT_BUTTON_PRESS
;
83 if ((p_slot
->state
== BLINKINGON_STATE
)
84 || (p_slot
->state
== BLINKINGOFF_STATE
)) {
85 /* Cancel if we are still blinking; this means that we press the
86 * attention again before the 5 sec. limit expires to cancel hot-add
89 taskInfo
->event_type
= INT_BUTTON_CANCEL
;
90 info("Button cancel on Slot(%d)\n", ctrl
->first_slot
+ hp_slot
);
91 } else if ((p_slot
->state
== POWERON_STATE
)
92 || (p_slot
->state
== POWEROFF_STATE
)) {
93 /* Ignore if the slot is on power-on or power-off state; this
94 * means that the previous attention button action to hot-add or
95 * hot-remove is undergoing
97 taskInfo
->event_type
= INT_BUTTON_IGNORE
;
98 info("Button ignore on Slot(%d)\n", ctrl
->first_slot
+ hp_slot
);
102 up(&event_semaphore
); /* signal event thread that new event is posted */
108 u8
shpchp_handle_switch_change(u8 hp_slot
, void *inst_id
)
110 struct controller
*ctrl
= (struct controller
*) inst_id
;
114 struct pci_func
*func
;
115 struct event_info
*taskInfo
;
118 dbg("shpchp: Switch interrupt received.\n");
120 func
= shpchp_slot_find(ctrl
->slot_bus
, (hp_slot
+ ctrl
->slot_device_offset
), 0);
122 /* This is the structure that tells the worker thread
125 taskInfo
= &(ctrl
->event_queue
[ctrl
->next_event
]);
126 ctrl
->next_event
= (ctrl
->next_event
+ 1) % 10;
127 taskInfo
->hp_slot
= hp_slot
;
130 p_slot
= shpchp_find_slot(ctrl
, hp_slot
+ ctrl
->slot_device_offset
);
131 p_slot
->hpc_ops
->get_adapter_status(p_slot
, &(func
->presence_save
));
132 p_slot
->hpc_ops
->get_latch_status(p_slot
, &getstatus
);
133 dbg("%s: Card present %x Power status %x\n", __FUNCTION__
,
134 func
->presence_save
, func
->pwr_save
);
140 info("Latch open on Slot(%d)\n", ctrl
->first_slot
+ hp_slot
);
141 func
->switch_save
= 0;
142 taskInfo
->event_type
= INT_SWITCH_OPEN
;
143 if (func
->pwr_save
&& func
->presence_save
) {
144 taskInfo
->event_type
= INT_POWER_FAULT
;
145 err("Surprise Removal of card\n");
151 info("Latch close on Slot(%d)\n", ctrl
->first_slot
+ hp_slot
);
152 func
->switch_save
= 0x10;
153 taskInfo
->event_type
= INT_SWITCH_CLOSE
;
157 up(&event_semaphore
); /* signal event thread that new event is posted */
162 u8
shpchp_handle_presence_change(u8 hp_slot
, void *inst_id
)
164 struct controller
*ctrl
= (struct controller
*) inst_id
;
168 struct pci_func
*func
;
169 struct event_info
*taskInfo
;
171 /* Presence Change */
172 dbg("shpchp: Presence/Notify input change.\n");
174 func
= shpchp_slot_find(ctrl
->slot_bus
, (hp_slot
+ ctrl
->slot_device_offset
), 0);
176 /* This is the structure that tells the worker thread
179 taskInfo
= &(ctrl
->event_queue
[ctrl
->next_event
]);
180 ctrl
->next_event
= (ctrl
->next_event
+ 1) % 10;
181 taskInfo
->hp_slot
= hp_slot
;
184 p_slot
= shpchp_find_slot(ctrl
, hp_slot
+ ctrl
->slot_device_offset
);
187 * Save the presence state
189 p_slot
->hpc_ops
->get_adapter_status(p_slot
, &(func
->presence_save
));
190 if (func
->presence_save
) {
194 info("Card present on Slot(%d)\n", ctrl
->first_slot
+ hp_slot
);
195 taskInfo
->event_type
= INT_PRESENCE_ON
;
200 info("Card not present on Slot(%d)\n", ctrl
->first_slot
+ hp_slot
);
201 taskInfo
->event_type
= INT_PRESENCE_OFF
;
205 up(&event_semaphore
); /* signal event thread that new event is posted */
210 u8
shpchp_handle_power_fault(u8 hp_slot
, void *inst_id
)
212 struct controller
*ctrl
= (struct controller
*) inst_id
;
215 struct pci_func
*func
;
216 struct event_info
*taskInfo
;
219 dbg("shpchp: Power fault interrupt received.\n");
221 func
= shpchp_slot_find(ctrl
->slot_bus
, (hp_slot
+ ctrl
->slot_device_offset
), 0);
223 /* This is the structure that tells the worker thread
226 taskInfo
= &(ctrl
->event_queue
[ctrl
->next_event
]);
227 ctrl
->next_event
= (ctrl
->next_event
+ 1) % 10;
228 taskInfo
->hp_slot
= hp_slot
;
231 p_slot
= shpchp_find_slot(ctrl
, hp_slot
+ ctrl
->slot_device_offset
);
233 if ( !(p_slot
->hpc_ops
->query_power_fault(p_slot
))) {
235 * Power fault Cleared
237 info("Power fault cleared on Slot(%d)\n", ctrl
->first_slot
+ hp_slot
);
239 taskInfo
->event_type
= INT_POWER_FAULT_CLEAR
;
244 info("Power fault on Slot(%d)\n", ctrl
->first_slot
+ hp_slot
);
245 taskInfo
->event_type
= INT_POWER_FAULT
;
246 /* set power fault status for this board */
248 info("power fault bit %x set\n", hp_slot
);
251 up(&event_semaphore
); /* signal event thread that new event is posted */
257 * shpchp_slot_create - Creates a node and adds it to the proper bus.
258 * @busnumber - bus where new node is to be located
260 * Returns pointer to the new node or NULL if unsuccessful
262 struct pci_func
*shpchp_slot_create(u8 busnumber
)
264 struct pci_func
*new_slot
;
265 struct pci_func
*next
;
267 new_slot
= kmalloc(sizeof(*new_slot
), GFP_KERNEL
);
269 if (new_slot
== NULL
) {
273 memset(new_slot
, 0, sizeof(struct pci_func
));
275 new_slot
->next
= NULL
;
276 new_slot
->configured
= 1;
278 if (shpchp_slot_list
[busnumber
] == NULL
) {
279 shpchp_slot_list
[busnumber
] = new_slot
;
281 next
= shpchp_slot_list
[busnumber
];
282 while (next
->next
!= NULL
)
284 next
->next
= new_slot
;
291 * slot_remove - Removes a node from the linked list of slots.
292 * @old_slot: slot to remove
294 * Returns 0 if successful, !0 otherwise.
296 static int slot_remove(struct pci_func
* old_slot
)
298 struct pci_func
*next
;
300 if (old_slot
== NULL
)
303 next
= shpchp_slot_list
[old_slot
->bus
];
309 if (next
== old_slot
) {
310 shpchp_slot_list
[old_slot
->bus
] = old_slot
->next
;
315 while ((next
->next
!= old_slot
) && (next
->next
!= NULL
)) {
319 if (next
->next
== old_slot
) {
320 next
->next
= old_slot
->next
;
329 * bridge_slot_remove - Removes a node from the linked list of slots.
330 * @bridge: bridge to remove
332 * Returns 0 if successful, !0 otherwise.
334 static int bridge_slot_remove(struct pci_func
*bridge
)
336 u8 subordinateBus
, secondaryBus
;
338 struct pci_func
*next
;
343 secondaryBus
= (bridge
->config_space
[0x06] >> 8) & 0xFF;
344 subordinateBus
= (bridge
->config_space
[0x06] >> 16) & 0xFF;
346 for (tempBus
= secondaryBus
; tempBus
<= subordinateBus
; tempBus
++) {
347 next
= shpchp_slot_list
[tempBus
];
349 while (!slot_remove(next
)) {
350 next
= shpchp_slot_list
[tempBus
];
354 next
= shpchp_slot_list
[bridge
->bus
];
360 if (next
== bridge
) {
361 shpchp_slot_list
[bridge
->bus
] = bridge
->next
;
366 while ((next
->next
!= bridge
) && (next
->next
!= NULL
)) {
370 if (next
->next
== bridge
) {
371 next
->next
= bridge
->next
;
380 * shpchp_slot_find - Looks for a node by bus, and device, multiple functions accessed
382 * @device: device to find
383 * @index: is 0 for first function found, 1 for the second...
385 * Returns pointer to the node if successful, %NULL otherwise.
387 struct pci_func
*shpchp_slot_find(u8 bus
, u8 device
, u8 index
)
390 struct pci_func
*func
;
392 func
= shpchp_slot_list
[bus
];
394 if ((func
== NULL
) || ((func
->device
== device
) && (index
== 0)))
397 if (func
->device
== device
)
400 while (func
->next
!= NULL
) {
403 if (func
->device
== device
)
413 static int is_bridge(struct pci_func
* func
)
415 /* Check the header type */
416 if (((func
->config_space
[0x03] >> 16) & 0xFF) == 0x01)
423 /* The following routines constitute the bulk of the
424 hotplug controller logic
426 static u32
change_bus_speed(struct controller
*ctrl
, struct slot
*p_slot
, enum pci_bus_speed speed
)
430 dbg("%s: change to speed %d\n", __FUNCTION__
, speed
);
431 down(&ctrl
->crit_sect
);
432 if ((rc
= p_slot
->hpc_ops
->set_bus_speed_mode(p_slot
, speed
))) {
433 err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__
);
434 up(&ctrl
->crit_sect
);
435 return WRONG_BUS_FREQUENCY
;
437 wait_for_ctrl_irq (ctrl
);
439 if ((rc
= p_slot
->hpc_ops
->check_cmd_status(ctrl
))) {
440 err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
442 err("%s: Error code (%d)\n", __FUNCTION__
, rc
);
443 up(&ctrl
->crit_sect
);
444 return WRONG_BUS_FREQUENCY
;
446 up(&ctrl
->crit_sect
);
450 static u32
fix_bus_speed(struct controller
*ctrl
, struct slot
*pslot
, u8 flag
,
451 enum pci_bus_speed asp
, enum pci_bus_speed bsp
, enum pci_bus_speed msp
)
455 if (flag
!= 0) { /* Other slots on the same bus are occupied */
457 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__
, bsp
, asp
);
458 return WRONG_BUS_FREQUENCY
;
461 /* Other slots on the same bus are empty */
463 /* if adapter_speed >= bus_speed, do nothing */
466 * Try to lower bus speed to accommodate the adapter if other slots
467 * on the same controller are empty
469 if ((rc
= change_bus_speed(ctrl
, pslot
, asp
)))
474 if ((rc
= change_bus_speed(ctrl
, pslot
, asp
)))
477 if ((rc
= change_bus_speed(ctrl
, pslot
, msp
)))
486 * board_added - Called after a board has been added to the system.
488 * Turns power on for the board
492 static u32
board_added(struct pci_func
* func
, struct controller
* ctrl
)
495 u8 slots_not_empty
= 0;
498 enum pci_bus_speed adapter_speed
, bus_speed
, max_bus_speed
;
501 p_slot
= shpchp_find_slot(ctrl
, func
->device
);
502 hp_slot
= func
->device
- ctrl
->slot_device_offset
;
504 dbg("%s: func->device, slot_offset, hp_slot = %d, %d ,%d\n", __FUNCTION__
, func
->device
, ctrl
->slot_device_offset
, hp_slot
);
506 /* Wait for exclusive access to hardware */
507 down(&ctrl
->crit_sect
);
509 /* Power on slot without connecting to bus */
510 rc
= p_slot
->hpc_ops
->power_on_slot(p_slot
);
512 err("%s: Failed to power on slot\n", __FUNCTION__
);
513 /* Done with exclusive hardware access */
514 up(&ctrl
->crit_sect
);
518 /* Wait for the command to complete */
519 wait_for_ctrl_irq (ctrl
);
521 rc
= p_slot
->hpc_ops
->check_cmd_status(ctrl
);
523 err("%s: Failed to power on slot, error code(%d)\n", __FUNCTION__
, rc
);
524 /* Done with exclusive hardware access */
525 up(&ctrl
->crit_sect
);
530 if ((ctrl
->pci_dev
->vendor
== 0x8086) && (ctrl
->pci_dev
->device
== 0x0332)) {
532 return WRONG_BUS_FREQUENCY
;
534 if ((rc
= p_slot
->hpc_ops
->set_bus_speed_mode(p_slot
, PCI_SPEED_33MHz
))) {
535 err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__
);
536 up(&ctrl
->crit_sect
);
537 return WRONG_BUS_FREQUENCY
;
539 wait_for_ctrl_irq (ctrl
);
541 if ((rc
= p_slot
->hpc_ops
->check_cmd_status(ctrl
))) {
542 err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
544 err("%s: Error code (%d)\n", __FUNCTION__
, rc
);
545 up(&ctrl
->crit_sect
);
546 return WRONG_BUS_FREQUENCY
;
548 /* turn on board, blink green LED, turn off Amber LED */
549 if ((rc
= p_slot
->hpc_ops
->slot_enable(p_slot
))) {
550 err("%s: Issue of Slot Enable command failed\n", __FUNCTION__
);
551 up(&ctrl
->crit_sect
);
554 wait_for_ctrl_irq (ctrl
);
556 if ((rc
= p_slot
->hpc_ops
->check_cmd_status(ctrl
))) {
557 err("%s: Failed to enable slot, error code(%d)\n", __FUNCTION__
, rc
);
558 up(&ctrl
->crit_sect
);
563 rc
= p_slot
->hpc_ops
->get_adapter_speed(p_slot
, &adapter_speed
);
564 /* 0 = PCI 33Mhz, 1 = PCI 66 Mhz, 2 = PCI-X 66 PA, 4 = PCI-X 66 ECC, */
565 /* 5 = PCI-X 133 PA, 7 = PCI-X 133 ECC, 0xa = PCI-X 133 Mhz 266, */
566 /* 0xd = PCI-X 133 Mhz 533 */
567 /* This encoding is different from the one used in cur_bus_speed & */
570 if (rc
|| adapter_speed
== PCI_SPEED_UNKNOWN
) {
571 err("%s: Can't get adapter speed or bus mode mismatch\n", __FUNCTION__
);
572 /* Done with exclusive hardware access */
573 up(&ctrl
->crit_sect
);
574 return WRONG_BUS_FREQUENCY
;
577 rc
= p_slot
->hpc_ops
->get_cur_bus_speed(p_slot
, &bus_speed
);
578 if (rc
|| bus_speed
== PCI_SPEED_UNKNOWN
) {
579 err("%s: Can't get bus operation speed\n", __FUNCTION__
);
580 /* Done with exclusive hardware access */
581 up(&ctrl
->crit_sect
);
582 return WRONG_BUS_FREQUENCY
;
585 rc
= p_slot
->hpc_ops
->get_max_bus_speed(p_slot
, &max_bus_speed
);
586 if (rc
|| max_bus_speed
== PCI_SPEED_UNKNOWN
) {
587 err("%s: Can't get max bus operation speed\n", __FUNCTION__
);
588 max_bus_speed
= bus_speed
;
591 /* Done with exclusive hardware access */
592 up(&ctrl
->crit_sect
);
594 if ((rc
= p_slot
->hpc_ops
->get_prog_int(p_slot
, &pi
))) {
595 err("%s: Can't get controller programming interface, set it to 1\n", __FUNCTION__
);
599 /* Check if there are other slots or devices on the same bus */
600 if (!list_empty(&ctrl
->pci_dev
->subordinate
->devices
))
603 dbg("%s: slots_not_empty %d, pi %d\n", __FUNCTION__
,
604 slots_not_empty
, pi
);
605 dbg("adapter_speed %d, bus_speed %d, max_bus_speed %d\n",
606 adapter_speed
, bus_speed
, max_bus_speed
);
609 dbg("%s: In PI = %d\n", __FUNCTION__
, pi
);
610 if ((rc
= p_slot
->hpc_ops
->get_mode1_ECC_cap(p_slot
, &mode
))) {
611 err("%s: Can't get Mode1_ECC, set mode to 0\n", __FUNCTION__
);
615 switch (adapter_speed
) {
616 case PCI_SPEED_133MHz_PCIX_533
:
617 case PCI_SPEED_133MHz_PCIX_266
:
618 if ((bus_speed
!= adapter_speed
) &&
619 ((rc
= fix_bus_speed(ctrl
, p_slot
, slots_not_empty
, adapter_speed
, bus_speed
, max_bus_speed
))))
622 case PCI_SPEED_133MHz_PCIX_ECC
:
623 case PCI_SPEED_133MHz_PCIX
:
624 if (mode
) { /* Bus - Mode 1 ECC */
625 if ((bus_speed
!= 0x7) &&
626 ((rc
= fix_bus_speed(ctrl
, p_slot
, slots_not_empty
, adapter_speed
, bus_speed
, max_bus_speed
))))
629 if ((bus_speed
!= 0x4) &&
630 ((rc
= fix_bus_speed(ctrl
, p_slot
, slots_not_empty
, adapter_speed
, bus_speed
, max_bus_speed
))))
634 case PCI_SPEED_66MHz_PCIX_ECC
:
635 case PCI_SPEED_66MHz_PCIX
:
636 if (mode
) { /* Bus - Mode 1 ECC */
637 if ((bus_speed
!= 0x5) &&
638 ((rc
= fix_bus_speed(ctrl
, p_slot
, slots_not_empty
, adapter_speed
, bus_speed
, max_bus_speed
))))
641 if ((bus_speed
!= 0x2) &&
642 ((rc
= fix_bus_speed(ctrl
, p_slot
, slots_not_empty
, adapter_speed
, bus_speed
, max_bus_speed
))))
646 case PCI_SPEED_66MHz
:
647 if ((bus_speed
!= 0x1) &&
648 ((rc
= fix_bus_speed(ctrl
, p_slot
, slots_not_empty
, adapter_speed
, bus_speed
, max_bus_speed
))))
651 case PCI_SPEED_33MHz
:
652 if (bus_speed
> 0x0) {
653 if (slots_not_empty
== 0) {
654 if ((rc
= change_bus_speed(ctrl
, p_slot
, adapter_speed
)))
657 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__
, bus_speed
, adapter_speed
);
658 return WRONG_BUS_FREQUENCY
;
663 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__
, bus_speed
, adapter_speed
);
664 return WRONG_BUS_FREQUENCY
;
667 /* If adpater_speed == bus_speed, nothing to do here */
668 dbg("%s: In PI = %d\n", __FUNCTION__
, pi
);
669 if ((adapter_speed
!= bus_speed
) &&
670 ((rc
= fix_bus_speed(ctrl
, p_slot
, slots_not_empty
, adapter_speed
, bus_speed
, max_bus_speed
))))
674 down(&ctrl
->crit_sect
);
675 /* turn on board, blink green LED, turn off Amber LED */
676 if ((rc
= p_slot
->hpc_ops
->slot_enable(p_slot
))) {
677 err("%s: Issue of Slot Enable command failed\n", __FUNCTION__
);
678 up(&ctrl
->crit_sect
);
681 wait_for_ctrl_irq (ctrl
);
683 if ((rc
= p_slot
->hpc_ops
->check_cmd_status(ctrl
))) {
684 err("%s: Failed to enable slot, error code(%d)\n", __FUNCTION__
, rc
);
685 up(&ctrl
->crit_sect
);
689 up(&ctrl
->crit_sect
);
691 /* Wait for ~1 second */
692 dbg("%s: before long_delay\n", __FUNCTION__
);
693 wait_for_ctrl_irq (ctrl
);
694 dbg("%s: after long_delay\n", __FUNCTION__
);
696 dbg("%s: func status = %x\n", __FUNCTION__
, func
->status
);
697 /* Check for a power fault */
698 if (func
->status
== 0xFF) {
699 /* power fault occurred, but it was benign */
700 dbg("%s: power fault\n", __FUNCTION__
);
706 if (shpchp_configure_device(p_slot
)) {
707 err("Cannot add device at 0x%x:0x%x\n", p_slot
->bus
,
712 shpchp_save_slot_config(ctrl
, func
);
715 func
->switch_save
= 0x10;
716 func
->is_a_board
= 0x01;
719 /* Wait for exclusive access to hardware */
720 down(&ctrl
->crit_sect
);
722 p_slot
->hpc_ops
->green_led_on(p_slot
);
724 /* Wait for the command to complete */
725 wait_for_ctrl_irq (ctrl
);
727 /* Done with exclusive hardware access */
728 up(&ctrl
->crit_sect
);
733 /* Wait for exclusive access to hardware */
734 down(&ctrl
->crit_sect
);
736 /* turn off slot, turn on Amber LED, turn off Green LED */
737 rc
= p_slot
->hpc_ops
->slot_disable(p_slot
);
739 err("%s: Issue of Slot Disable command failed\n", __FUNCTION__
);
740 /* Done with exclusive hardware access */
741 up(&ctrl
->crit_sect
);
744 /* Wait for the command to complete */
745 wait_for_ctrl_irq (ctrl
);
747 rc
= p_slot
->hpc_ops
->check_cmd_status(ctrl
);
749 err("%s: Failed to disable slot, error code(%d)\n", __FUNCTION__
, rc
);
750 /* Done with exclusive hardware access */
751 up(&ctrl
->crit_sect
);
755 /* Done with exclusive hardware access */
756 up(&ctrl
->crit_sect
);
763 * remove_board - Turns off slot and LED's
766 static u32
remove_board(struct pci_func
*func
, struct controller
*ctrl
)
776 if (shpchp_unconfigure_device(func
))
779 device
= func
->device
;
781 hp_slot
= func
->device
- ctrl
->slot_device_offset
;
782 p_slot
= shpchp_find_slot(ctrl
, hp_slot
+ ctrl
->slot_device_offset
);
784 dbg("In %s, hp_slot = %d\n", __FUNCTION__
, hp_slot
);
786 /* Change status to shutdown */
787 if (func
->is_a_board
)
789 func
->configured
= 0;
791 /* Wait for exclusive access to hardware */
792 down(&ctrl
->crit_sect
);
794 /* turn off slot, turn on Amber LED, turn off Green LED */
795 rc
= p_slot
->hpc_ops
->slot_disable(p_slot
);
797 err("%s: Issue of Slot Disable command failed\n", __FUNCTION__
);
798 /* Done with exclusive hardware access */
799 up(&ctrl
->crit_sect
);
802 /* Wait for the command to complete */
803 wait_for_ctrl_irq (ctrl
);
805 rc
= p_slot
->hpc_ops
->check_cmd_status(ctrl
);
807 err("%s: Failed to disable slot, error code(%d)\n", __FUNCTION__
, rc
);
808 /* Done with exclusive hardware access */
809 up(&ctrl
->crit_sect
);
813 rc
= p_slot
->hpc_ops
->set_attention_status(p_slot
, 0);
815 err("%s: Issue of Set Attention command failed\n", __FUNCTION__
);
816 /* Done with exclusive hardware access */
817 up(&ctrl
->crit_sect
);
820 /* Wait for the command to complete */
821 wait_for_ctrl_irq (ctrl
);
823 /* Done with exclusive hardware access */
824 up(&ctrl
->crit_sect
);
826 if (ctrl
->add_support
) {
828 if (is_bridge(func
)) {
829 dbg("PCI Bridge Hot-Remove s:b:d:f(%02x:%02x:%02x:%02x)\n", ctrl
->seg
, func
->bus
,
830 func
->device
, func
->function
);
831 bridge_slot_remove(func
);
833 dbg("PCI Function Hot-Remove s:b:d:f(%02x:%02x:%02x:%02x)\n", ctrl
->seg
, func
->bus
,
834 func
->device
, func
->function
);
837 func
= shpchp_slot_find(ctrl
->slot_bus
, device
, 0);
840 /* Setup slot structure with entry for empty slot */
841 func
= shpchp_slot_create(ctrl
->slot_bus
);
847 func
->bus
= ctrl
->slot_bus
;
848 func
->device
= device
;
850 func
->configured
= 0;
851 func
->switch_save
= 0x10;
853 func
->is_a_board
= 0;
860 static void pushbutton_helper_thread (unsigned long data
)
862 pushbutton_pending
= data
;
864 up(&event_semaphore
);
869 * shpchp_pushbutton_thread
871 * Scheduled procedure to handle blocking stuff for the pushbuttons
872 * Handles all pending events and exits.
875 static void shpchp_pushbutton_thread (unsigned long slot
)
877 struct slot
*p_slot
= (struct slot
*) slot
;
880 pushbutton_pending
= 0;
883 dbg("%s: Error! slot NULL\n", __FUNCTION__
);
887 p_slot
->hpc_ops
->get_power_status(p_slot
, &getstatus
);
889 p_slot
->state
= POWEROFF_STATE
;
890 dbg("In power_down_board, b:d(%x:%x)\n", p_slot
->bus
, p_slot
->device
);
892 shpchp_disable_slot(p_slot
);
893 p_slot
->state
= STATIC_STATE
;
895 p_slot
->state
= POWERON_STATE
;
896 dbg("In add_board, b:d(%x:%x)\n", p_slot
->bus
, p_slot
->device
);
898 if (shpchp_enable_slot(p_slot
)) {
899 /* Wait for exclusive access to hardware */
900 down(&p_slot
->ctrl
->crit_sect
);
902 p_slot
->hpc_ops
->green_led_off(p_slot
);
904 /* Wait for the command to complete */
905 wait_for_ctrl_irq (p_slot
->ctrl
);
907 /* Done with exclusive hardware access */
908 up(&p_slot
->ctrl
->crit_sect
);
910 p_slot
->state
= STATIC_STATE
;
917 /* this is the main worker thread */
918 static int event_thread(void* data
)
920 struct controller
*ctrl
;
922 daemonize("shpchpd_event");
926 dbg("!!!!event_thread sleeping\n");
927 down_interruptible (&event_semaphore
);
928 dbg("event_thread woken finished = %d\n", event_finished
);
929 if (event_finished
|| signal_pending(current
))
932 if (pushbutton_pending
)
933 shpchp_pushbutton_thread(pushbutton_pending
);
935 for (ctrl
= shpchp_ctrl_list
; ctrl
; ctrl
=ctrl
->next
)
936 interrupt_event_handler(ctrl
);
938 dbg("event_thread signals exit\n");
943 int shpchp_event_start_thread (void)
947 /* initialize our semaphores */
948 init_MUTEX_LOCKED(&event_exit
);
951 init_MUTEX_LOCKED(&event_semaphore
);
952 pid
= kernel_thread(event_thread
, NULL
, 0);
955 err ("Can't start up our event thread\n");
958 dbg("Our event thread pid = %d\n", pid
);
963 void shpchp_event_stop_thread (void)
966 dbg("event_thread finish command given\n");
967 up(&event_semaphore
);
968 dbg("wait for event_thread to exit\n");
973 static int update_slot_info (struct slot
*slot
)
975 struct hotplug_slot_info
*info
;
978 info
= kmalloc(sizeof(*info
), GFP_KERNEL
);
982 slot
->hpc_ops
->get_power_status(slot
, &(info
->power_status
));
983 slot
->hpc_ops
->get_attention_status(slot
, &(info
->attention_status
));
984 slot
->hpc_ops
->get_latch_status(slot
, &(info
->latch_status
));
985 slot
->hpc_ops
->get_adapter_status(slot
, &(info
->adapter_status
));
987 result
= pci_hp_change_slot_info(slot
->hotplug_slot
, info
);
992 static void interrupt_event_handler(struct controller
*ctrl
)
996 struct pci_func
*func
;
1001 dbg("%s:\n", __FUNCTION__
);
1005 for (loop
= 0; loop
< 10; loop
++) {
1006 if (ctrl
->event_queue
[loop
].event_type
!= 0) {
1007 dbg("%s:loop %x event_type %x\n", __FUNCTION__
, loop
,
1008 ctrl
->event_queue
[loop
].event_type
);
1009 hp_slot
= ctrl
->event_queue
[loop
].hp_slot
;
1011 func
= shpchp_slot_find(ctrl
->slot_bus
, (hp_slot
+ ctrl
->slot_device_offset
), 0);
1013 p_slot
= shpchp_find_slot(ctrl
, hp_slot
+ ctrl
->slot_device_offset
);
1015 dbg("%s: hp_slot %d, func %p, p_slot %p\n", __FUNCTION__
, hp_slot
, func
, p_slot
);
1017 if (ctrl
->event_queue
[loop
].event_type
== INT_BUTTON_CANCEL
) {
1018 dbg("%s: button cancel\n", __FUNCTION__
);
1019 del_timer(&p_slot
->task_event
);
1021 switch (p_slot
->state
) {
1022 case BLINKINGOFF_STATE
:
1023 /* Wait for exclusive access to hardware */
1024 down(&ctrl
->crit_sect
);
1026 p_slot
->hpc_ops
->green_led_on(p_slot
);
1027 /* Wait for the command to complete */
1028 wait_for_ctrl_irq (ctrl
);
1030 p_slot
->hpc_ops
->set_attention_status(p_slot
, 0);
1032 /* Wait for the command to complete */
1033 wait_for_ctrl_irq (ctrl
);
1035 /* Done with exclusive hardware access */
1036 up(&ctrl
->crit_sect
);
1038 case BLINKINGON_STATE
:
1039 /* Wait for exclusive access to hardware */
1040 down(&ctrl
->crit_sect
);
1042 p_slot
->hpc_ops
->green_led_off(p_slot
);
1043 /* Wait for the command to complete */
1044 wait_for_ctrl_irq (ctrl
);
1046 p_slot
->hpc_ops
->set_attention_status(p_slot
, 0);
1047 /* Wait for the command to complete */
1048 wait_for_ctrl_irq (ctrl
);
1050 /* Done with exclusive hardware access */
1051 up(&ctrl
->crit_sect
);
1055 warn("Not a valid state\n");
1058 info(msg_button_cancel
, p_slot
->number
);
1059 p_slot
->state
= STATIC_STATE
;
1060 } else if (ctrl
->event_queue
[loop
].event_type
== INT_BUTTON_PRESS
) {
1061 /* Button Pressed (No action on 1st press...) */
1062 dbg("%s: Button pressed\n", __FUNCTION__
);
1064 p_slot
->hpc_ops
->get_power_status(p_slot
, &getstatus
);
1067 dbg("%s: slot is on\n", __FUNCTION__
);
1068 p_slot
->state
= BLINKINGOFF_STATE
;
1069 info(msg_button_off
, p_slot
->number
);
1072 dbg("%s: slot is off\n", __FUNCTION__
);
1073 p_slot
->state
= BLINKINGON_STATE
;
1074 info(msg_button_on
, p_slot
->number
);
1077 /* Wait for exclusive access to hardware */
1078 down(&ctrl
->crit_sect
);
1080 /* blink green LED and turn off amber */
1081 p_slot
->hpc_ops
->green_led_blink(p_slot
);
1082 /* Wait for the command to complete */
1083 wait_for_ctrl_irq (ctrl
);
1085 p_slot
->hpc_ops
->set_attention_status(p_slot
, 0);
1087 /* Wait for the command to complete */
1088 wait_for_ctrl_irq (ctrl
);
1090 /* Done with exclusive hardware access */
1091 up(&ctrl
->crit_sect
);
1093 init_timer(&p_slot
->task_event
);
1094 p_slot
->task_event
.expires
= jiffies
+ 5 * HZ
; /* 5 second delay */
1095 p_slot
->task_event
.function
= (void (*)(unsigned long)) pushbutton_helper_thread
;
1096 p_slot
->task_event
.data
= (unsigned long) p_slot
;
1098 dbg("%s: add_timer p_slot = %p\n", __FUNCTION__
,(void *) p_slot
);
1099 add_timer(&p_slot
->task_event
);
1100 } else if (ctrl
->event_queue
[loop
].event_type
== INT_POWER_FAULT
) {
1101 /***********POWER FAULT********************/
1102 dbg("%s: power fault\n", __FUNCTION__
);
1103 /* Wait for exclusive access to hardware */
1104 down(&ctrl
->crit_sect
);
1106 p_slot
->hpc_ops
->set_attention_status(p_slot
, 1);
1107 /* Wait for the command to complete */
1108 wait_for_ctrl_irq (ctrl
);
1110 p_slot
->hpc_ops
->green_led_off(p_slot
);
1111 /* Wait for the command to complete */
1112 wait_for_ctrl_irq (ctrl
);
1114 /* Done with exclusive hardware access */
1115 up(&ctrl
->crit_sect
);
1117 /* refresh notification */
1119 update_slot_info(p_slot
);
1122 ctrl
->event_queue
[loop
].event_type
= 0;
1126 } /* End of FOR loop */
1133 int shpchp_enable_slot (struct slot
*p_slot
)
1137 struct pci_func
*func
;
1139 func
= shpchp_slot_find(p_slot
->bus
, p_slot
->device
, 0);
1141 dbg("%s: Error! slot NULL\n", __FUNCTION__
);
1145 /* Check to see if (latch closed, card present, power off) */
1146 down(&p_slot
->ctrl
->crit_sect
);
1147 rc
= p_slot
->hpc_ops
->get_adapter_status(p_slot
, &getstatus
);
1148 if (rc
|| !getstatus
) {
1149 info("%s: no adapter on slot(%x)\n", __FUNCTION__
, p_slot
->number
);
1150 up(&p_slot
->ctrl
->crit_sect
);
1153 rc
= p_slot
->hpc_ops
->get_latch_status(p_slot
, &getstatus
);
1154 if (rc
|| getstatus
) {
1155 info("%s: latch open on slot(%x)\n", __FUNCTION__
, p_slot
->number
);
1156 up(&p_slot
->ctrl
->crit_sect
);
1159 rc
= p_slot
->hpc_ops
->get_power_status(p_slot
, &getstatus
);
1160 if (rc
|| getstatus
) {
1161 info("%s: already enabled on slot(%x)\n", __FUNCTION__
, p_slot
->number
);
1162 up(&p_slot
->ctrl
->crit_sect
);
1165 up(&p_slot
->ctrl
->crit_sect
);
1169 func
= shpchp_slot_create(p_slot
->bus
);
1173 func
->bus
= p_slot
->bus
;
1174 func
->device
= p_slot
->device
;
1176 func
->configured
= 0;
1177 func
->is_a_board
= 1;
1179 /* We have to save the presence info for these slots */
1180 p_slot
->hpc_ops
->get_adapter_status(p_slot
, &(func
->presence_save
));
1181 p_slot
->hpc_ops
->get_power_status(p_slot
, &(func
->pwr_save
));
1182 dbg("%s: func->pwr_save %x\n", __FUNCTION__
, func
->pwr_save
);
1183 p_slot
->hpc_ops
->get_latch_status(p_slot
, &getstatus
);
1184 func
->switch_save
= !getstatus
? 0x10:0;
1186 rc
= board_added(func
, p_slot
->ctrl
);
1188 if (is_bridge(func
))
1189 bridge_slot_remove(func
);
1193 /* Setup slot structure with entry for empty slot */
1194 func
= shpchp_slot_create(p_slot
->bus
);
1196 return -ENOMEM
; /* Out of memory */
1198 func
->bus
= p_slot
->bus
;
1199 func
->device
= p_slot
->device
;
1201 func
->configured
= 0;
1202 func
->is_a_board
= 1;
1204 /* We have to save the presence info for these slots */
1205 p_slot
->hpc_ops
->get_adapter_status(p_slot
, &(func
->presence_save
));
1206 p_slot
->hpc_ops
->get_latch_status(p_slot
, &getstatus
);
1207 func
->switch_save
= !getstatus
? 0x10:0;
1211 update_slot_info(p_slot
);
1217 int shpchp_disable_slot (struct slot
*p_slot
)
1219 u8 class_code
, header_type
, BCR
;
1225 struct pci_bus
*pci_bus
;
1226 struct pci_func
*func
;
1231 pci_bus
= p_slot
->ctrl
->pci_dev
->subordinate
;
1233 /* Check to see if (latch closed, card present, power on) */
1234 down(&p_slot
->ctrl
->crit_sect
);
1236 ret
= p_slot
->hpc_ops
->get_adapter_status(p_slot
, &getstatus
);
1237 if (ret
|| !getstatus
) {
1238 info("%s: no adapter on slot(%x)\n", __FUNCTION__
, p_slot
->number
);
1239 up(&p_slot
->ctrl
->crit_sect
);
1242 ret
= p_slot
->hpc_ops
->get_latch_status(p_slot
, &getstatus
);
1243 if (ret
|| getstatus
) {
1244 info("%s: latch open on slot(%x)\n", __FUNCTION__
, p_slot
->number
);
1245 up(&p_slot
->ctrl
->crit_sect
);
1248 ret
= p_slot
->hpc_ops
->get_power_status(p_slot
, &getstatus
);
1249 if (ret
|| !getstatus
) {
1250 info("%s: already disabled slot(%x)\n", __FUNCTION__
, p_slot
->number
);
1251 up(&p_slot
->ctrl
->crit_sect
);
1254 up(&p_slot
->ctrl
->crit_sect
);
1256 func
= shpchp_slot_find(p_slot
->bus
, p_slot
->device
, index
++);
1258 /* Make sure there are no video controllers here
1259 * for all func of p_slot
1261 while (func
&& !rc
) {
1262 pci_bus
->number
= func
->bus
;
1263 devfn
= PCI_DEVFN(func
->device
, func
->function
);
1265 /* Check the Class Code */
1266 rc
= pci_bus_read_config_byte (pci_bus
, devfn
, 0x0B, &class_code
);
1270 if (class_code
== PCI_BASE_CLASS_DISPLAY
) {
1271 /* Display/Video adapter (not supported) */
1272 rc
= REMOVE_NOT_SUPPORTED
;
1274 /* See if it's a bridge */
1275 rc
= pci_bus_read_config_byte (pci_bus
, devfn
, PCI_HEADER_TYPE
, &header_type
);
1279 /* If it's a bridge, check the VGA Enable bit */
1280 if ((header_type
& 0x7F) == PCI_HEADER_TYPE_BRIDGE
) {
1281 rc
= pci_bus_read_config_byte (pci_bus
, devfn
, PCI_BRIDGE_CONTROL
, &BCR
);
1285 /* If the VGA Enable bit is set, remove isn't supported */
1286 if (BCR
& PCI_BRIDGE_CTL_VGA
) {
1287 rc
= REMOVE_NOT_SUPPORTED
;
1292 func
= shpchp_slot_find(p_slot
->bus
, p_slot
->device
, index
++);
1295 func
= shpchp_slot_find(p_slot
->bus
, p_slot
->device
, 0);
1296 if ((func
!= NULL
) && !rc
) {
1297 rc
= remove_board(func
, p_slot
->ctrl
);
1302 update_slot_info(p_slot
);