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
331 * @secondaryBus: secondary PCI bus number for bridge being removed
332 * @subordinateBus: subordinate PCI bus number for bridge being removed
334 * Returns 0 if successful, !0 otherwise.
336 static int bridge_slot_remove(struct pci_func *bridge, u8 secondaryBus,
340 struct pci_func *next;
345 for (tempBus = secondaryBus; tempBus <= subordinateBus; tempBus++) {
346 next = shpchp_slot_list[tempBus];
348 while (!slot_remove(next)) {
349 next = shpchp_slot_list[tempBus];
353 next = shpchp_slot_list[bridge->bus];
359 if (next == bridge) {
360 shpchp_slot_list[bridge->bus] = bridge->next;
365 while ((next->next != bridge) && (next->next != NULL)) {
369 if (next->next == bridge) {
370 next->next = bridge->next;
379 * shpchp_slot_find - Looks for a node by bus, and device, multiple functions accessed
381 * @device: device to find
382 * @index: is 0 for first function found, 1 for the second...
384 * Returns pointer to the node if successful, %NULL otherwise.
386 struct pci_func *shpchp_slot_find(u8 bus, u8 device, u8 index)
389 struct pci_func *func;
391 func = shpchp_slot_list[bus];
393 if ((func == NULL) || ((func->device == device) && (index == 0)))
396 if (func->device == device)
399 while (func->next != NULL) {
402 if (func->device == device)
412 static int is_bridge(struct pci_func *func, struct controller *ctrl)
415 struct pci_bus *bus = ctrl->pci_dev->subordinate;
418 * Note: device may have just been hot-added and not yet scanned
419 * by the pci core, so its pci_dev structure may not exist yet
421 pci_bus_read_config_byte(bus, PCI_DEVFN(func->device, func->function),
422 PCI_HEADER_TYPE, &hdr_type);
423 if ((hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE)
429 /* The following routines constitute the bulk of the
430 hotplug controller logic
432 static u32 change_bus_speed(struct controller *ctrl, struct slot *p_slot, enum pci_bus_speed speed)
436 dbg("%s: change to speed %d\n", __FUNCTION__, speed);
437 down(&ctrl->crit_sect);
438 if ((rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, speed))) {
439 err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
440 up(&ctrl->crit_sect);
441 return WRONG_BUS_FREQUENCY;
443 wait_for_ctrl_irq (ctrl);
445 if ((rc = p_slot->hpc_ops->check_cmd_status(ctrl))) {
446 err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
448 err("%s: Error code (%d)\n", __FUNCTION__, rc);
449 up(&ctrl->crit_sect);
450 return WRONG_BUS_FREQUENCY;
452 up(&ctrl->crit_sect);
456 static u32 fix_bus_speed(struct controller *ctrl, struct slot *pslot, u8 flag,
457 enum pci_bus_speed asp, enum pci_bus_speed bsp, enum pci_bus_speed msp)
461 if (flag != 0) { /* Other slots on the same bus are occupied */
463 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bsp, asp);
464 return WRONG_BUS_FREQUENCY;
467 /* Other slots on the same bus are empty */
469 /* if adapter_speed >= bus_speed, do nothing */
472 * Try to lower bus speed to accommodate the adapter if other slots
473 * on the same controller are empty
475 if ((rc = change_bus_speed(ctrl, pslot, asp)))
480 if ((rc = change_bus_speed(ctrl, pslot, asp)))
483 if ((rc = change_bus_speed(ctrl, pslot, msp)))
492 * board_added - Called after a board has been added to the system.
494 * Turns power on for the board
498 static u32 board_added(struct pci_func * func, struct controller * ctrl)
501 u8 slots_not_empty = 0;
504 enum pci_bus_speed adapter_speed, bus_speed, max_bus_speed;
507 p_slot = shpchp_find_slot(ctrl, func->device);
508 hp_slot = func->device - ctrl->slot_device_offset;
510 dbg("%s: func->device, slot_offset, hp_slot = %d, %d ,%d\n", __FUNCTION__, func->device, ctrl->slot_device_offset, hp_slot);
512 /* Wait for exclusive access to hardware */
513 down(&ctrl->crit_sect);
515 /* Power on slot without connecting to bus */
516 rc = p_slot->hpc_ops->power_on_slot(p_slot);
518 err("%s: Failed to power on slot\n", __FUNCTION__);
519 /* Done with exclusive hardware access */
520 up(&ctrl->crit_sect);
524 /* Wait for the command to complete */
525 wait_for_ctrl_irq (ctrl);
527 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
529 err("%s: Failed to power on slot, error code(%d)\n", __FUNCTION__, rc);
530 /* Done with exclusive hardware access */
531 up(&ctrl->crit_sect);
536 if ((ctrl->pci_dev->vendor == 0x8086) && (ctrl->pci_dev->device == 0x0332)) {
538 return WRONG_BUS_FREQUENCY;
540 if ((rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, PCI_SPEED_33MHz))) {
541 err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
542 up(&ctrl->crit_sect);
543 return WRONG_BUS_FREQUENCY;
545 wait_for_ctrl_irq (ctrl);
547 if ((rc = p_slot->hpc_ops->check_cmd_status(ctrl))) {
548 err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
550 err("%s: Error code (%d)\n", __FUNCTION__, rc);
551 up(&ctrl->crit_sect);
552 return WRONG_BUS_FREQUENCY;
554 /* turn on board, blink green LED, turn off Amber LED */
555 if ((rc = p_slot->hpc_ops->slot_enable(p_slot))) {
556 err("%s: Issue of Slot Enable command failed\n", __FUNCTION__);
557 up(&ctrl->crit_sect);
560 wait_for_ctrl_irq (ctrl);
562 if ((rc = p_slot->hpc_ops->check_cmd_status(ctrl))) {
563 err("%s: Failed to enable slot, error code(%d)\n", __FUNCTION__, rc);
564 up(&ctrl->crit_sect);
569 rc = p_slot->hpc_ops->get_adapter_speed(p_slot, &adapter_speed);
570 /* 0 = PCI 33Mhz, 1 = PCI 66 Mhz, 2 = PCI-X 66 PA, 4 = PCI-X 66 ECC, */
571 /* 5 = PCI-X 133 PA, 7 = PCI-X 133 ECC, 0xa = PCI-X 133 Mhz 266, */
572 /* 0xd = PCI-X 133 Mhz 533 */
573 /* This encoding is different from the one used in cur_bus_speed & */
576 if (rc || adapter_speed == PCI_SPEED_UNKNOWN) {
577 err("%s: Can't get adapter speed or bus mode mismatch\n", __FUNCTION__);
578 /* Done with exclusive hardware access */
579 up(&ctrl->crit_sect);
580 return WRONG_BUS_FREQUENCY;
583 rc = p_slot->hpc_ops->get_cur_bus_speed(p_slot, &bus_speed);
584 if (rc || bus_speed == PCI_SPEED_UNKNOWN) {
585 err("%s: Can't get bus operation speed\n", __FUNCTION__);
586 /* Done with exclusive hardware access */
587 up(&ctrl->crit_sect);
588 return WRONG_BUS_FREQUENCY;
591 rc = p_slot->hpc_ops->get_max_bus_speed(p_slot, &max_bus_speed);
592 if (rc || max_bus_speed == PCI_SPEED_UNKNOWN) {
593 err("%s: Can't get max bus operation speed\n", __FUNCTION__);
594 max_bus_speed = bus_speed;
597 /* Done with exclusive hardware access */
598 up(&ctrl->crit_sect);
600 if ((rc = p_slot->hpc_ops->get_prog_int(p_slot, &pi))) {
601 err("%s: Can't get controller programming interface, set it to 1\n", __FUNCTION__);
605 /* Check if there are other slots or devices on the same bus */
606 if (!list_empty(&ctrl->pci_dev->subordinate->devices))
609 dbg("%s: slots_not_empty %d, pi %d\n", __FUNCTION__,
610 slots_not_empty, pi);
611 dbg("adapter_speed %d, bus_speed %d, max_bus_speed %d\n",
612 adapter_speed, bus_speed, max_bus_speed);
615 dbg("%s: In PI = %d\n", __FUNCTION__, pi);
616 if ((rc = p_slot->hpc_ops->get_mode1_ECC_cap(p_slot, &mode))) {
617 err("%s: Can't get Mode1_ECC, set mode to 0\n", __FUNCTION__);
621 switch (adapter_speed) {
622 case PCI_SPEED_133MHz_PCIX_533:
623 case PCI_SPEED_133MHz_PCIX_266:
624 if ((bus_speed != adapter_speed) &&
625 ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
628 case PCI_SPEED_133MHz_PCIX_ECC:
629 case PCI_SPEED_133MHz_PCIX:
630 if (mode) { /* Bus - Mode 1 ECC */
631 if ((bus_speed != 0x7) &&
632 ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
635 if ((bus_speed != 0x4) &&
636 ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
640 case PCI_SPEED_66MHz_PCIX_ECC:
641 case PCI_SPEED_66MHz_PCIX:
642 if (mode) { /* Bus - Mode 1 ECC */
643 if ((bus_speed != 0x5) &&
644 ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
647 if ((bus_speed != 0x2) &&
648 ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
652 case PCI_SPEED_66MHz:
653 if ((bus_speed != 0x1) &&
654 ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
657 case PCI_SPEED_33MHz:
658 if (bus_speed > 0x0) {
659 if (slots_not_empty == 0) {
660 if ((rc = change_bus_speed(ctrl, p_slot, adapter_speed)))
663 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
664 return WRONG_BUS_FREQUENCY;
669 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
670 return WRONG_BUS_FREQUENCY;
673 /* If adpater_speed == bus_speed, nothing to do here */
674 dbg("%s: In PI = %d\n", __FUNCTION__, pi);
675 if ((adapter_speed != bus_speed) &&
676 ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
680 down(&ctrl->crit_sect);
681 /* turn on board, blink green LED, turn off Amber LED */
682 if ((rc = p_slot->hpc_ops->slot_enable(p_slot))) {
683 err("%s: Issue of Slot Enable command failed\n", __FUNCTION__);
684 up(&ctrl->crit_sect);
687 wait_for_ctrl_irq (ctrl);
689 if ((rc = p_slot->hpc_ops->check_cmd_status(ctrl))) {
690 err("%s: Failed to enable slot, error code(%d)\n", __FUNCTION__, rc);
691 up(&ctrl->crit_sect);
695 up(&ctrl->crit_sect);
697 /* Wait for ~1 second */
698 dbg("%s: before long_delay\n", __FUNCTION__);
699 wait_for_ctrl_irq (ctrl);
700 dbg("%s: after long_delay\n", __FUNCTION__);
702 dbg("%s: func status = %x\n", __FUNCTION__, func->status);
703 /* Check for a power fault */
704 if (func->status == 0xFF) {
705 /* power fault occurred, but it was benign */
706 dbg("%s: power fault\n", __FUNCTION__);
712 if (shpchp_configure_device(p_slot)) {
713 err("Cannot add device at 0x%x:0x%x\n", p_slot->bus,
719 func->switch_save = 0x10;
720 func->is_a_board = 0x01;
723 /* Wait for exclusive access to hardware */
724 down(&ctrl->crit_sect);
726 p_slot->hpc_ops->green_led_on(p_slot);
728 /* Wait for the command to complete */
729 wait_for_ctrl_irq (ctrl);
731 /* Done with exclusive hardware access */
732 up(&ctrl->crit_sect);
737 /* Wait for exclusive access to hardware */
738 down(&ctrl->crit_sect);
740 /* turn off slot, turn on Amber LED, turn off Green LED */
741 rc = p_slot->hpc_ops->slot_disable(p_slot);
743 err("%s: Issue of Slot Disable command failed\n", __FUNCTION__);
744 /* Done with exclusive hardware access */
745 up(&ctrl->crit_sect);
748 /* Wait for the command to complete */
749 wait_for_ctrl_irq (ctrl);
751 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
753 err("%s: Failed to disable slot, error code(%d)\n", __FUNCTION__, rc);
754 /* Done with exclusive hardware access */
755 up(&ctrl->crit_sect);
759 /* Done with exclusive hardware access */
760 up(&ctrl->crit_sect);
767 * remove_board - Turns off slot and LED's
770 static u32 remove_board(struct pci_func *func, struct controller *ctrl)
776 u8 secondary = 0, subordinate = 0;
782 if ((remove_bridge = is_bridge(func, ctrl))) {
783 /* Stash away bus information before we destroy it */
784 secondary = func->pci_dev->subordinate->secondary;
785 subordinate = func->pci_dev->subordinate->subordinate;
788 if (shpchp_unconfigure_device(func))
791 device = func->device;
793 hp_slot = func->device - ctrl->slot_device_offset;
794 p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
796 dbg("In %s, hp_slot = %d\n", __FUNCTION__, hp_slot);
798 /* Change status to shutdown */
799 if (func->is_a_board)
801 func->configured = 0;
803 /* Wait for exclusive access to hardware */
804 down(&ctrl->crit_sect);
806 /* turn off slot, turn on Amber LED, turn off Green LED */
807 rc = p_slot->hpc_ops->slot_disable(p_slot);
809 err("%s: Issue of Slot Disable command failed\n", __FUNCTION__);
810 /* Done with exclusive hardware access */
811 up(&ctrl->crit_sect);
814 /* Wait for the command to complete */
815 wait_for_ctrl_irq (ctrl);
817 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
819 err("%s: Failed to disable slot, error code(%d)\n", __FUNCTION__, rc);
820 /* Done with exclusive hardware access */
821 up(&ctrl->crit_sect);
825 rc = p_slot->hpc_ops->set_attention_status(p_slot, 0);
827 err("%s: Issue of Set Attention command failed\n", __FUNCTION__);
828 /* Done with exclusive hardware access */
829 up(&ctrl->crit_sect);
832 /* Wait for the command to complete */
833 wait_for_ctrl_irq (ctrl);
835 /* Done with exclusive hardware access */
836 up(&ctrl->crit_sect);
838 if (ctrl->add_support) {
841 dbg("PCI Bridge Hot-Remove s:b:d:f(%02x:%02x:%02x:%02x)\n", ctrl->seg, func->bus,
842 func->device, func->function);
843 bridge_slot_remove(func, secondary,
846 dbg("PCI Function Hot-Remove s:b:d:f(%02x:%02x:%02x:%02x)\n", ctrl->seg, func->bus,
847 func->device, func->function);
850 func = shpchp_slot_find(ctrl->slot_bus, device, 0);
853 /* Setup slot structure with entry for empty slot */
854 func = shpchp_slot_create(ctrl->slot_bus);
860 func->bus = ctrl->slot_bus;
861 func->device = device;
863 func->configured = 0;
864 func->switch_save = 0x10;
866 func->is_a_board = 0;
873 static void pushbutton_helper_thread (unsigned long data)
875 pushbutton_pending = data;
877 up(&event_semaphore);
882 * shpchp_pushbutton_thread
884 * Scheduled procedure to handle blocking stuff for the pushbuttons
885 * Handles all pending events and exits.
888 static void shpchp_pushbutton_thread (unsigned long slot)
890 struct slot *p_slot = (struct slot *) slot;
893 pushbutton_pending = 0;
896 dbg("%s: Error! slot NULL\n", __FUNCTION__);
900 p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
902 p_slot->state = POWEROFF_STATE;
903 dbg("In power_down_board, b:d(%x:%x)\n", p_slot->bus, p_slot->device);
905 shpchp_disable_slot(p_slot);
906 p_slot->state = STATIC_STATE;
908 p_slot->state = POWERON_STATE;
909 dbg("In add_board, b:d(%x:%x)\n", p_slot->bus, p_slot->device);
911 if (shpchp_enable_slot(p_slot)) {
912 /* Wait for exclusive access to hardware */
913 down(&p_slot->ctrl->crit_sect);
915 p_slot->hpc_ops->green_led_off(p_slot);
917 /* Wait for the command to complete */
918 wait_for_ctrl_irq (p_slot->ctrl);
920 /* Done with exclusive hardware access */
921 up(&p_slot->ctrl->crit_sect);
923 p_slot->state = STATIC_STATE;
930 /* this is the main worker thread */
931 static int event_thread(void* data)
933 struct controller *ctrl;
935 daemonize("shpchpd_event");
939 dbg("!!!!event_thread sleeping\n");
940 down_interruptible (&event_semaphore);
941 dbg("event_thread woken finished = %d\n", event_finished);
942 if (event_finished || signal_pending(current))
945 if (pushbutton_pending)
946 shpchp_pushbutton_thread(pushbutton_pending);
948 for (ctrl = shpchp_ctrl_list; ctrl; ctrl=ctrl->next)
949 interrupt_event_handler(ctrl);
951 dbg("event_thread signals exit\n");
956 int shpchp_event_start_thread (void)
960 /* initialize our semaphores */
961 init_MUTEX_LOCKED(&event_exit);
964 init_MUTEX_LOCKED(&event_semaphore);
965 pid = kernel_thread(event_thread, NULL, 0);
968 err ("Can't start up our event thread\n");
971 dbg("Our event thread pid = %d\n", pid);
976 void shpchp_event_stop_thread (void)
979 dbg("event_thread finish command given\n");
980 up(&event_semaphore);
981 dbg("wait for event_thread to exit\n");
986 static int update_slot_info (struct slot *slot)
988 struct hotplug_slot_info *info;
991 info = kmalloc(sizeof(*info), GFP_KERNEL);
995 slot->hpc_ops->get_power_status(slot, &(info->power_status));
996 slot->hpc_ops->get_attention_status(slot, &(info->attention_status));
997 slot->hpc_ops->get_latch_status(slot, &(info->latch_status));
998 slot->hpc_ops->get_adapter_status(slot, &(info->adapter_status));
1000 result = pci_hp_change_slot_info(slot->hotplug_slot, info);
1005 static void interrupt_event_handler(struct controller *ctrl)
1009 struct pci_func *func;
1012 struct slot *p_slot;
1014 dbg("%s:\n", __FUNCTION__);
1018 for (loop = 0; loop < 10; loop++) {
1019 if (ctrl->event_queue[loop].event_type != 0) {
1020 dbg("%s:loop %x event_type %x\n", __FUNCTION__, loop,
1021 ctrl->event_queue[loop].event_type);
1022 hp_slot = ctrl->event_queue[loop].hp_slot;
1024 func = shpchp_slot_find(ctrl->slot_bus, (hp_slot + ctrl->slot_device_offset), 0);
1026 p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1028 dbg("%s: hp_slot %d, func %p, p_slot %p\n", __FUNCTION__, hp_slot, func, p_slot);
1030 if (ctrl->event_queue[loop].event_type == INT_BUTTON_CANCEL) {
1031 dbg("%s: button cancel\n", __FUNCTION__);
1032 del_timer(&p_slot->task_event);
1034 switch (p_slot->state) {
1035 case BLINKINGOFF_STATE:
1036 /* Wait for exclusive access to hardware */
1037 down(&ctrl->crit_sect);
1039 p_slot->hpc_ops->green_led_on(p_slot);
1040 /* Wait for the command to complete */
1041 wait_for_ctrl_irq (ctrl);
1043 p_slot->hpc_ops->set_attention_status(p_slot, 0);
1045 /* Wait for the command to complete */
1046 wait_for_ctrl_irq (ctrl);
1048 /* Done with exclusive hardware access */
1049 up(&ctrl->crit_sect);
1051 case BLINKINGON_STATE:
1052 /* Wait for exclusive access to hardware */
1053 down(&ctrl->crit_sect);
1055 p_slot->hpc_ops->green_led_off(p_slot);
1056 /* Wait for the command to complete */
1057 wait_for_ctrl_irq (ctrl);
1059 p_slot->hpc_ops->set_attention_status(p_slot, 0);
1060 /* Wait for the command to complete */
1061 wait_for_ctrl_irq (ctrl);
1063 /* Done with exclusive hardware access */
1064 up(&ctrl->crit_sect);
1068 warn("Not a valid state\n");
1071 info(msg_button_cancel, p_slot->number);
1072 p_slot->state = STATIC_STATE;
1073 } else if (ctrl->event_queue[loop].event_type == INT_BUTTON_PRESS) {
1074 /* Button Pressed (No action on 1st press...) */
1075 dbg("%s: Button pressed\n", __FUNCTION__);
1077 p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
1080 dbg("%s: slot is on\n", __FUNCTION__);
1081 p_slot->state = BLINKINGOFF_STATE;
1082 info(msg_button_off, p_slot->number);
1085 dbg("%s: slot is off\n", __FUNCTION__);
1086 p_slot->state = BLINKINGON_STATE;
1087 info(msg_button_on, p_slot->number);
1090 /* Wait for exclusive access to hardware */
1091 down(&ctrl->crit_sect);
1093 /* blink green LED and turn off amber */
1094 p_slot->hpc_ops->green_led_blink(p_slot);
1095 /* Wait for the command to complete */
1096 wait_for_ctrl_irq (ctrl);
1098 p_slot->hpc_ops->set_attention_status(p_slot, 0);
1100 /* Wait for the command to complete */
1101 wait_for_ctrl_irq (ctrl);
1103 /* Done with exclusive hardware access */
1104 up(&ctrl->crit_sect);
1106 init_timer(&p_slot->task_event);
1107 p_slot->task_event.expires = jiffies + 5 * HZ; /* 5 second delay */
1108 p_slot->task_event.function = (void (*)(unsigned long)) pushbutton_helper_thread;
1109 p_slot->task_event.data = (unsigned long) p_slot;
1111 dbg("%s: add_timer p_slot = %p\n", __FUNCTION__,(void *) p_slot);
1112 add_timer(&p_slot->task_event);
1113 } else if (ctrl->event_queue[loop].event_type == INT_POWER_FAULT) {
1114 /***********POWER FAULT********************/
1115 dbg("%s: power fault\n", __FUNCTION__);
1116 /* Wait for exclusive access to hardware */
1117 down(&ctrl->crit_sect);
1119 p_slot->hpc_ops->set_attention_status(p_slot, 1);
1120 /* Wait for the command to complete */
1121 wait_for_ctrl_irq (ctrl);
1123 p_slot->hpc_ops->green_led_off(p_slot);
1124 /* Wait for the command to complete */
1125 wait_for_ctrl_irq (ctrl);
1127 /* Done with exclusive hardware access */
1128 up(&ctrl->crit_sect);
1130 /* refresh notification */
1132 update_slot_info(p_slot);
1135 ctrl->event_queue[loop].event_type = 0;
1139 } /* End of FOR loop */
1146 int shpchp_enable_slot (struct slot *p_slot)
1150 struct pci_func *func;
1152 func = shpchp_slot_find(p_slot->bus, p_slot->device, 0);
1154 dbg("%s: Error! slot NULL\n", __FUNCTION__);
1158 /* Check to see if (latch closed, card present, power off) */
1159 down(&p_slot->ctrl->crit_sect);
1160 rc = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
1161 if (rc || !getstatus) {
1162 info("%s: no adapter on slot(%x)\n", __FUNCTION__, p_slot->number);
1163 up(&p_slot->ctrl->crit_sect);
1166 rc = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
1167 if (rc || getstatus) {
1168 info("%s: latch open on slot(%x)\n", __FUNCTION__, p_slot->number);
1169 up(&p_slot->ctrl->crit_sect);
1172 rc = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
1173 if (rc || getstatus) {
1174 info("%s: already enabled on slot(%x)\n", __FUNCTION__, p_slot->number);
1175 up(&p_slot->ctrl->crit_sect);
1178 up(&p_slot->ctrl->crit_sect);
1182 func = shpchp_slot_create(p_slot->bus);
1186 func->bus = p_slot->bus;
1187 func->device = p_slot->device;
1189 func->configured = 0;
1190 func->is_a_board = 1;
1192 /* We have to save the presence info for these slots */
1193 p_slot->hpc_ops->get_adapter_status(p_slot, &(func->presence_save));
1194 p_slot->hpc_ops->get_power_status(p_slot, &(func->pwr_save));
1195 dbg("%s: func->pwr_save %x\n", __FUNCTION__, func->pwr_save);
1196 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
1197 func->switch_save = !getstatus? 0x10:0;
1199 rc = board_added(func, p_slot->ctrl);
1201 if (is_bridge(func, p_slot->ctrl)) {
1202 u8 secondary = func->pci_dev->subordinate->secondary;
1204 func->pci_dev->subordinate->subordinate;
1205 bridge_slot_remove(func, secondary, subordinate);
1209 /* Setup slot structure with entry for empty slot */
1210 func = shpchp_slot_create(p_slot->bus);
1212 return -ENOMEM; /* Out of memory */
1214 func->bus = p_slot->bus;
1215 func->device = p_slot->device;
1217 func->configured = 0;
1218 func->is_a_board = 1;
1220 /* We have to save the presence info for these slots */
1221 p_slot->hpc_ops->get_adapter_status(p_slot, &(func->presence_save));
1222 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
1223 func->switch_save = !getstatus? 0x10:0;
1227 update_slot_info(p_slot);
1233 int shpchp_disable_slot (struct slot *p_slot)
1235 u8 class_code, header_type, BCR;
1241 struct pci_bus *pci_bus;
1242 struct pci_func *func;
1247 pci_bus = p_slot->ctrl->pci_dev->subordinate;
1249 /* Check to see if (latch closed, card present, power on) */
1250 down(&p_slot->ctrl->crit_sect);
1252 ret = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
1253 if (ret || !getstatus) {
1254 info("%s: no adapter on slot(%x)\n", __FUNCTION__, p_slot->number);
1255 up(&p_slot->ctrl->crit_sect);
1258 ret = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
1259 if (ret || getstatus) {
1260 info("%s: latch open on slot(%x)\n", __FUNCTION__, p_slot->number);
1261 up(&p_slot->ctrl->crit_sect);
1264 ret = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
1265 if (ret || !getstatus) {
1266 info("%s: already disabled slot(%x)\n", __FUNCTION__, p_slot->number);
1267 up(&p_slot->ctrl->crit_sect);
1270 up(&p_slot->ctrl->crit_sect);
1272 func = shpchp_slot_find(p_slot->bus, p_slot->device, index++);
1274 /* Make sure there are no video controllers here
1275 * for all func of p_slot
1277 while (func && !rc) {
1278 pci_bus->number = func->bus;
1279 devfn = PCI_DEVFN(func->device, func->function);
1281 /* Check the Class Code */
1282 rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
1286 if (class_code == PCI_BASE_CLASS_DISPLAY) {
1287 /* Display/Video adapter (not supported) */
1288 rc = REMOVE_NOT_SUPPORTED;
1290 /* See if it's a bridge */
1291 rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
1295 /* If it's a bridge, check the VGA Enable bit */
1296 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
1297 rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_BRIDGE_CONTROL, &BCR);
1301 /* If the VGA Enable bit is set, remove isn't supported */
1302 if (BCR & PCI_BRIDGE_CTL_VGA) {
1303 rc = REMOVE_NOT_SUPPORTED;
1308 func = shpchp_slot_find(p_slot->bus, p_slot->device, index++);
1311 func = shpchp_slot_find(p_slot->bus, p_slot->device, 0);
1312 if ((func != NULL) && !rc) {
1313 rc = remove_board(func, p_slot->ctrl);
1318 update_slot_info(p_slot);