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>
45 static void interrupt_event_handler(struct controller *ctrl);
47 static struct semaphore event_semaphore; /* mutex for process loop (up if something to process) */
48 static struct semaphore event_exit; /* guard ensure thread has exited before calling it quits */
49 static int event_finished;
50 static unsigned long pushbutton_pending; /* = 0 */
52 u8 shpchp_handle_attention_button(u8 hp_slot, void *inst_id)
54 struct controller *ctrl = (struct controller *) inst_id;
58 struct pci_func *func;
59 struct event_info *taskInfo;
61 /* Attention Button Change */
62 dbg("shpchp: Attention button interrupt received.\n");
64 func = shpchp_slot_find(ctrl->slot_bus, (hp_slot + ctrl->slot_device_offset), 0);
66 /* This is the structure that tells the worker thread what to do */
67 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
68 p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
70 p_slot->hpc_ops->get_adapter_status(p_slot, &(func->presence_save));
71 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
73 ctrl->next_event = (ctrl->next_event + 1) % 10;
74 taskInfo->hp_slot = hp_slot;
79 * Button pressed - See if need to TAKE ACTION!!!
81 info("Button pressed on Slot(%d)\n", ctrl->first_slot + hp_slot);
82 taskInfo->event_type = INT_BUTTON_PRESS;
84 if ((p_slot->state == BLINKINGON_STATE)
85 || (p_slot->state == BLINKINGOFF_STATE)) {
86 /* Cancel if we are still blinking; this means that we press the
87 * attention again before the 5 sec. limit expires to cancel hot-add
90 taskInfo->event_type = INT_BUTTON_CANCEL;
91 info("Button cancel on Slot(%d)\n", ctrl->first_slot + hp_slot);
92 } else if ((p_slot->state == POWERON_STATE)
93 || (p_slot->state == POWEROFF_STATE)) {
94 /* Ignore if the slot is on power-on or power-off state; this
95 * means that the previous attention button action to hot-add or
96 * hot-remove is undergoing
98 taskInfo->event_type = INT_BUTTON_IGNORE;
99 info("Button ignore on Slot(%d)\n", ctrl->first_slot + hp_slot);
103 up(&event_semaphore); /* signal event thread that new event is posted */
109 u8 shpchp_handle_switch_change(u8 hp_slot, void *inst_id)
111 struct controller *ctrl = (struct controller *) inst_id;
115 struct pci_func *func;
116 struct event_info *taskInfo;
119 dbg("shpchp: Switch interrupt received.\n");
121 func = shpchp_slot_find(ctrl->slot_bus, (hp_slot + ctrl->slot_device_offset), 0);
123 /* This is the structure that tells the worker thread
126 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
127 ctrl->next_event = (ctrl->next_event + 1) % 10;
128 taskInfo->hp_slot = hp_slot;
131 p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
132 p_slot->hpc_ops->get_adapter_status(p_slot, &(func->presence_save));
133 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
134 dbg("%s: Card present %x Power status %x\n", __FUNCTION__,
135 func->presence_save, func->pwr_save);
141 info("Latch open on Slot(%d)\n", ctrl->first_slot + hp_slot);
142 func->switch_save = 0;
143 taskInfo->event_type = INT_SWITCH_OPEN;
144 if (func->pwr_save && func->presence_save) {
145 taskInfo->event_type = INT_POWER_FAULT;
146 err("Surprise Removal of card\n");
152 info("Latch close on Slot(%d)\n", ctrl->first_slot + hp_slot);
153 func->switch_save = 0x10;
154 taskInfo->event_type = INT_SWITCH_CLOSE;
158 up(&event_semaphore); /* signal event thread that new event is posted */
163 u8 shpchp_handle_presence_change(u8 hp_slot, void *inst_id)
165 struct controller *ctrl = (struct controller *) inst_id;
169 struct pci_func *func;
170 struct event_info *taskInfo;
172 /* Presence Change */
173 dbg("shpchp: Presence/Notify input change.\n");
175 func = shpchp_slot_find(ctrl->slot_bus, (hp_slot + ctrl->slot_device_offset), 0);
177 /* This is the structure that tells the worker thread
180 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
181 ctrl->next_event = (ctrl->next_event + 1) % 10;
182 taskInfo->hp_slot = hp_slot;
185 p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
188 * Save the presence state
190 p_slot->hpc_ops->get_adapter_status(p_slot, &(func->presence_save));
191 if (func->presence_save) {
195 info("Card present on Slot(%d)\n", ctrl->first_slot + hp_slot);
196 taskInfo->event_type = INT_PRESENCE_ON;
201 info("Card not present on Slot(%d)\n", ctrl->first_slot + hp_slot);
202 taskInfo->event_type = INT_PRESENCE_OFF;
206 up(&event_semaphore); /* signal event thread that new event is posted */
211 u8 shpchp_handle_power_fault(u8 hp_slot, void *inst_id)
213 struct controller *ctrl = (struct controller *) inst_id;
216 struct pci_func *func;
217 struct event_info *taskInfo;
220 dbg("shpchp: Power fault interrupt received.\n");
222 func = shpchp_slot_find(ctrl->slot_bus, (hp_slot + ctrl->slot_device_offset), 0);
224 /* This is the structure that tells the worker thread
227 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
228 ctrl->next_event = (ctrl->next_event + 1) % 10;
229 taskInfo->hp_slot = hp_slot;
232 p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
234 if ( !(p_slot->hpc_ops->query_power_fault(p_slot))) {
236 * Power fault Cleared
238 info("Power fault cleared on Slot(%d)\n", ctrl->first_slot + hp_slot);
240 taskInfo->event_type = INT_POWER_FAULT_CLEAR;
245 info("Power fault on Slot(%d)\n", ctrl->first_slot + hp_slot);
246 taskInfo->event_type = INT_POWER_FAULT;
247 /* set power fault status for this board */
249 info("power fault bit %x set\n", hp_slot);
252 up(&event_semaphore); /* signal event thread that new event is posted */
258 * shpchp_slot_create - Creates a node and adds it to the proper bus.
259 * @busnumber - bus where new node is to be located
261 * Returns pointer to the new node or NULL if unsuccessful
263 struct pci_func *shpchp_slot_create(u8 busnumber)
265 struct pci_func *new_slot;
266 struct pci_func *next;
268 new_slot = kmalloc(sizeof(*new_slot), GFP_KERNEL);
270 if (new_slot == NULL) {
274 memset(new_slot, 0, sizeof(struct pci_func));
276 new_slot->next = NULL;
277 new_slot->configured = 1;
279 if (shpchp_slot_list[busnumber] == NULL) {
280 shpchp_slot_list[busnumber] = new_slot;
282 next = shpchp_slot_list[busnumber];
283 while (next->next != NULL)
285 next->next = new_slot;
292 * slot_remove - Removes a node from the linked list of slots.
293 * @old_slot: slot to remove
295 * Returns 0 if successful, !0 otherwise.
297 static int slot_remove(struct pci_func * old_slot)
299 struct pci_func *next;
301 if (old_slot == NULL)
304 next = shpchp_slot_list[old_slot->bus];
310 if (next == old_slot) {
311 shpchp_slot_list[old_slot->bus] = old_slot->next;
316 while ((next->next != old_slot) && (next->next != NULL)) {
320 if (next->next == old_slot) {
321 next->next = old_slot->next;
330 * bridge_slot_remove - Removes a node from the linked list of slots.
331 * @bridge: bridge to remove
333 * Returns 0 if successful, !0 otherwise.
335 static int bridge_slot_remove(struct pci_func *bridge)
337 u8 subordinateBus, secondaryBus;
339 struct pci_func *next;
344 secondaryBus = (bridge->config_space[0x06] >> 8) & 0xFF;
345 subordinateBus = (bridge->config_space[0x06] >> 16) & 0xFF;
347 for (tempBus = secondaryBus; tempBus <= subordinateBus; tempBus++) {
348 next = shpchp_slot_list[tempBus];
350 while (!slot_remove(next)) {
351 next = shpchp_slot_list[tempBus];
355 next = shpchp_slot_list[bridge->bus];
361 if (next == bridge) {
362 shpchp_slot_list[bridge->bus] = bridge->next;
367 while ((next->next != bridge) && (next->next != NULL)) {
371 if (next->next == bridge) {
372 next->next = bridge->next;
381 * shpchp_slot_find - Looks for a node by bus, and device, multiple functions accessed
383 * @device: device to find
384 * @index: is 0 for first function found, 1 for the second...
386 * Returns pointer to the node if successful, %NULL otherwise.
388 struct pci_func *shpchp_slot_find(u8 bus, u8 device, u8 index)
391 struct pci_func *func;
393 func = shpchp_slot_list[bus];
395 if ((func == NULL) || ((func->device == device) && (index == 0)))
398 if (func->device == device)
401 while (func->next != NULL) {
404 if (func->device == device)
414 static int is_bridge(struct pci_func * func)
416 /* Check the header type */
417 if (((func->config_space[0x03] >> 16) & 0xFF) == 0x01)
424 /* The following routines constitute the bulk of the
425 hotplug controller logic
427 static u32 change_bus_speed(struct controller *ctrl, struct slot *p_slot, enum pci_bus_speed speed)
431 dbg("%s: change to speed %d\n", __FUNCTION__, speed);
432 down(&ctrl->crit_sect);
433 if ((rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, speed))) {
434 err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
435 up(&ctrl->crit_sect);
436 return WRONG_BUS_FREQUENCY;
438 wait_for_ctrl_irq (ctrl);
440 if ((rc = p_slot->hpc_ops->check_cmd_status(ctrl))) {
441 err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
443 err("%s: Error code (%d)\n", __FUNCTION__, rc);
444 up(&ctrl->crit_sect);
445 return WRONG_BUS_FREQUENCY;
447 up(&ctrl->crit_sect);
451 static u32 fix_bus_speed(struct controller *ctrl, struct slot *pslot, u8 flag,
452 enum pci_bus_speed asp, enum pci_bus_speed bsp, enum pci_bus_speed msp)
456 if (flag != 0) { /* Other slots on the same bus are occupied */
458 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bsp, asp);
459 return WRONG_BUS_FREQUENCY;
462 /* Other slots on the same bus are empty */
464 /* if adapter_speed >= bus_speed, do nothing */
467 * Try to lower bus speed to accommodate the adapter if other slots
468 * on the same controller are empty
470 if ((rc = change_bus_speed(ctrl, pslot, asp)))
475 if ((rc = change_bus_speed(ctrl, pslot, asp)))
478 if ((rc = change_bus_speed(ctrl, pslot, msp)))
487 * board_added - Called after a board has been added to the system.
489 * Turns power on for the board
493 static u32 board_added(struct pci_func * func, struct controller * ctrl)
496 u8 slots_not_empty = 0;
499 enum pci_bus_speed adapter_speed, bus_speed, max_bus_speed;
502 p_slot = shpchp_find_slot(ctrl, func->device);
503 hp_slot = func->device - ctrl->slot_device_offset;
505 dbg("%s: func->device, slot_offset, hp_slot = %d, %d ,%d\n", __FUNCTION__, func->device, ctrl->slot_device_offset, hp_slot);
507 /* Wait for exclusive access to hardware */
508 down(&ctrl->crit_sect);
510 /* Power on slot without connecting to bus */
511 rc = p_slot->hpc_ops->power_on_slot(p_slot);
513 err("%s: Failed to power on slot\n", __FUNCTION__);
514 /* Done with exclusive hardware access */
515 up(&ctrl->crit_sect);
519 /* Wait for the command to complete */
520 wait_for_ctrl_irq (ctrl);
522 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
524 err("%s: Failed to power on slot, error code(%d)\n", __FUNCTION__, rc);
525 /* Done with exclusive hardware access */
526 up(&ctrl->crit_sect);
531 if ((ctrl->pci_dev->vendor == 0x8086) && (ctrl->pci_dev->device == 0x0332)) {
533 return WRONG_BUS_FREQUENCY;
535 if ((rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, PCI_SPEED_33MHz))) {
536 err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
537 up(&ctrl->crit_sect);
538 return WRONG_BUS_FREQUENCY;
540 wait_for_ctrl_irq (ctrl);
542 if ((rc = p_slot->hpc_ops->check_cmd_status(ctrl))) {
543 err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
545 err("%s: Error code (%d)\n", __FUNCTION__, rc);
546 up(&ctrl->crit_sect);
547 return WRONG_BUS_FREQUENCY;
549 /* turn on board, blink green LED, turn off Amber LED */
550 if ((rc = p_slot->hpc_ops->slot_enable(p_slot))) {
551 err("%s: Issue of Slot Enable command failed\n", __FUNCTION__);
552 up(&ctrl->crit_sect);
555 wait_for_ctrl_irq (ctrl);
557 if ((rc = p_slot->hpc_ops->check_cmd_status(ctrl))) {
558 err("%s: Failed to enable slot, error code(%d)\n", __FUNCTION__, rc);
559 up(&ctrl->crit_sect);
564 rc = p_slot->hpc_ops->get_adapter_speed(p_slot, &adapter_speed);
565 /* 0 = PCI 33Mhz, 1 = PCI 66 Mhz, 2 = PCI-X 66 PA, 4 = PCI-X 66 ECC, */
566 /* 5 = PCI-X 133 PA, 7 = PCI-X 133 ECC, 0xa = PCI-X 133 Mhz 266, */
567 /* 0xd = PCI-X 133 Mhz 533 */
568 /* This encoding is different from the one used in cur_bus_speed & */
571 if (rc || adapter_speed == PCI_SPEED_UNKNOWN) {
572 err("%s: Can't get adapter speed or bus mode mismatch\n", __FUNCTION__);
573 /* Done with exclusive hardware access */
574 up(&ctrl->crit_sect);
575 return WRONG_BUS_FREQUENCY;
578 rc = p_slot->hpc_ops->get_cur_bus_speed(p_slot, &bus_speed);
579 if (rc || bus_speed == PCI_SPEED_UNKNOWN) {
580 err("%s: Can't get bus operation speed\n", __FUNCTION__);
581 /* Done with exclusive hardware access */
582 up(&ctrl->crit_sect);
583 return WRONG_BUS_FREQUENCY;
586 rc = p_slot->hpc_ops->get_max_bus_speed(p_slot, &max_bus_speed);
587 if (rc || max_bus_speed == PCI_SPEED_UNKNOWN) {
588 err("%s: Can't get max bus operation speed\n", __FUNCTION__);
589 max_bus_speed = bus_speed;
592 /* Done with exclusive hardware access */
593 up(&ctrl->crit_sect);
595 if ((rc = p_slot->hpc_ops->get_prog_int(p_slot, &pi))) {
596 err("%s: Can't get controller programming interface, set it to 1\n", __FUNCTION__);
600 /* Check if there are other slots or devices on the same bus */
601 if (!list_empty(&ctrl->pci_dev->subordinate->devices))
604 dbg("%s: slots_not_empty %d, pi %d\n", __FUNCTION__,
605 slots_not_empty, pi);
606 dbg("adapter_speed %d, bus_speed %d, max_bus_speed %d\n",
607 adapter_speed, bus_speed, max_bus_speed);
610 dbg("%s: In PI = %d\n", __FUNCTION__, pi);
611 if ((rc = p_slot->hpc_ops->get_mode1_ECC_cap(p_slot, &mode))) {
612 err("%s: Can't get Mode1_ECC, set mode to 0\n", __FUNCTION__);
616 switch (adapter_speed) {
617 case PCI_SPEED_133MHz_PCIX_533:
618 case PCI_SPEED_133MHz_PCIX_266:
619 if ((bus_speed != adapter_speed) &&
620 ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
623 case PCI_SPEED_133MHz_PCIX_ECC:
624 case PCI_SPEED_133MHz_PCIX:
625 if (mode) { /* Bus - Mode 1 ECC */
626 if ((bus_speed != 0x7) &&
627 ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
630 if ((bus_speed != 0x4) &&
631 ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
635 case PCI_SPEED_66MHz_PCIX_ECC:
636 case PCI_SPEED_66MHz_PCIX:
637 if (mode) { /* Bus - Mode 1 ECC */
638 if ((bus_speed != 0x5) &&
639 ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
642 if ((bus_speed != 0x2) &&
643 ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
647 case PCI_SPEED_66MHz:
648 if ((bus_speed != 0x1) &&
649 ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
652 case PCI_SPEED_33MHz:
653 if (bus_speed > 0x0) {
654 if (slots_not_empty == 0) {
655 if ((rc = change_bus_speed(ctrl, p_slot, adapter_speed)))
658 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
659 return WRONG_BUS_FREQUENCY;
664 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
665 return WRONG_BUS_FREQUENCY;
668 /* If adpater_speed == bus_speed, nothing to do here */
669 dbg("%s: In PI = %d\n", __FUNCTION__, pi);
670 if ((adapter_speed != bus_speed) &&
671 ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
675 down(&ctrl->crit_sect);
676 /* turn on board, blink green LED, turn off Amber LED */
677 if ((rc = p_slot->hpc_ops->slot_enable(p_slot))) {
678 err("%s: Issue of Slot Enable command failed\n", __FUNCTION__);
679 up(&ctrl->crit_sect);
682 wait_for_ctrl_irq (ctrl);
684 if ((rc = p_slot->hpc_ops->check_cmd_status(ctrl))) {
685 err("%s: Failed to enable slot, error code(%d)\n", __FUNCTION__, rc);
686 up(&ctrl->crit_sect);
690 up(&ctrl->crit_sect);
692 /* Wait for ~1 second */
693 dbg("%s: before long_delay\n", __FUNCTION__);
694 wait_for_ctrl_irq (ctrl);
695 dbg("%s: after long_delay\n", __FUNCTION__);
697 dbg("%s: func status = %x\n", __FUNCTION__, func->status);
698 /* Check for a power fault */
699 if (func->status == 0xFF) {
700 /* power fault occurred, but it was benign */
701 dbg("%s: power fault\n", __FUNCTION__);
707 if (shpchp_configure_device(p_slot)) {
708 err("Cannot add device at 0x%x:0x%x\n", p_slot->bus,
713 shpchp_save_slot_config(ctrl, func);
716 func->switch_save = 0x10;
717 func->is_a_board = 0x01;
720 /* Wait for exclusive access to hardware */
721 down(&ctrl->crit_sect);
723 p_slot->hpc_ops->green_led_on(p_slot);
725 /* Wait for the command to complete */
726 wait_for_ctrl_irq (ctrl);
728 /* Done with exclusive hardware access */
729 up(&ctrl->crit_sect);
734 /* Wait for exclusive access to hardware */
735 down(&ctrl->crit_sect);
737 /* turn off slot, turn on Amber LED, turn off Green LED */
738 rc = p_slot->hpc_ops->slot_disable(p_slot);
740 err("%s: Issue of Slot Disable command failed\n", __FUNCTION__);
741 /* Done with exclusive hardware access */
742 up(&ctrl->crit_sect);
745 /* Wait for the command to complete */
746 wait_for_ctrl_irq (ctrl);
748 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
750 err("%s: Failed to disable slot, error code(%d)\n", __FUNCTION__, rc);
751 /* Done with exclusive hardware access */
752 up(&ctrl->crit_sect);
756 /* Done with exclusive hardware access */
757 up(&ctrl->crit_sect);
764 * remove_board - Turns off slot and LED's
767 static u32 remove_board(struct pci_func *func, struct controller *ctrl)
777 if (shpchp_unconfigure_device(func))
780 device = func->device;
782 hp_slot = func->device - ctrl->slot_device_offset;
783 p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
785 dbg("In %s, hp_slot = %d\n", __FUNCTION__, hp_slot);
787 /* Change status to shutdown */
788 if (func->is_a_board)
790 func->configured = 0;
792 /* Wait for exclusive access to hardware */
793 down(&ctrl->crit_sect);
795 /* turn off slot, turn on Amber LED, turn off Green LED */
796 rc = p_slot->hpc_ops->slot_disable(p_slot);
798 err("%s: Issue of Slot Disable command failed\n", __FUNCTION__);
799 /* Done with exclusive hardware access */
800 up(&ctrl->crit_sect);
803 /* Wait for the command to complete */
804 wait_for_ctrl_irq (ctrl);
806 rc = p_slot->hpc_ops->check_cmd_status(ctrl);
808 err("%s: Failed to disable slot, error code(%d)\n", __FUNCTION__, rc);
809 /* Done with exclusive hardware access */
810 up(&ctrl->crit_sect);
814 rc = p_slot->hpc_ops->set_attention_status(p_slot, 0);
816 err("%s: Issue of Set Attention command failed\n", __FUNCTION__);
817 /* Done with exclusive hardware access */
818 up(&ctrl->crit_sect);
821 /* Wait for the command to complete */
822 wait_for_ctrl_irq (ctrl);
824 /* Done with exclusive hardware access */
825 up(&ctrl->crit_sect);
827 if (ctrl->add_support) {
829 if (is_bridge(func)) {
830 dbg("PCI Bridge Hot-Remove s:b:d:f(%02x:%02x:%02x:%02x)\n", ctrl->seg, func->bus,
831 func->device, func->function);
832 bridge_slot_remove(func);
834 dbg("PCI Function Hot-Remove s:b:d:f(%02x:%02x:%02x:%02x)\n", ctrl->seg, func->bus,
835 func->device, func->function);
838 func = shpchp_slot_find(ctrl->slot_bus, device, 0);
841 /* Setup slot structure with entry for empty slot */
842 func = shpchp_slot_create(ctrl->slot_bus);
848 func->bus = ctrl->slot_bus;
849 func->device = device;
851 func->configured = 0;
852 func->switch_save = 0x10;
854 func->is_a_board = 0;
861 static void pushbutton_helper_thread (unsigned long data)
863 pushbutton_pending = data;
865 up(&event_semaphore);
870 * shpchp_pushbutton_thread
872 * Scheduled procedure to handle blocking stuff for the pushbuttons
873 * Handles all pending events and exits.
876 static void shpchp_pushbutton_thread (unsigned long slot)
878 struct slot *p_slot = (struct slot *) slot;
881 pushbutton_pending = 0;
884 dbg("%s: Error! slot NULL\n", __FUNCTION__);
888 p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
890 p_slot->state = POWEROFF_STATE;
891 dbg("In power_down_board, b:d(%x:%x)\n", p_slot->bus, p_slot->device);
893 shpchp_disable_slot(p_slot);
894 p_slot->state = STATIC_STATE;
896 p_slot->state = POWERON_STATE;
897 dbg("In add_board, b:d(%x:%x)\n", p_slot->bus, p_slot->device);
899 if (shpchp_enable_slot(p_slot)) {
900 /* Wait for exclusive access to hardware */
901 down(&p_slot->ctrl->crit_sect);
903 p_slot->hpc_ops->green_led_off(p_slot);
905 /* Wait for the command to complete */
906 wait_for_ctrl_irq (p_slot->ctrl);
908 /* Done with exclusive hardware access */
909 up(&p_slot->ctrl->crit_sect);
911 p_slot->state = STATIC_STATE;
918 /* this is the main worker thread */
919 static int event_thread(void* data)
921 struct controller *ctrl;
923 daemonize("shpchpd_event");
927 dbg("!!!!event_thread sleeping\n");
928 down_interruptible (&event_semaphore);
929 dbg("event_thread woken finished = %d\n", event_finished);
930 if (event_finished || signal_pending(current))
933 if (pushbutton_pending)
934 shpchp_pushbutton_thread(pushbutton_pending);
936 for (ctrl = shpchp_ctrl_list; ctrl; ctrl=ctrl->next)
937 interrupt_event_handler(ctrl);
939 dbg("event_thread signals exit\n");
944 int shpchp_event_start_thread (void)
948 /* initialize our semaphores */
949 init_MUTEX_LOCKED(&event_exit);
952 init_MUTEX_LOCKED(&event_semaphore);
953 pid = kernel_thread(event_thread, NULL, 0);
956 err ("Can't start up our event thread\n");
959 dbg("Our event thread pid = %d\n", pid);
964 void shpchp_event_stop_thread (void)
967 dbg("event_thread finish command given\n");
968 up(&event_semaphore);
969 dbg("wait for event_thread to exit\n");
974 static int update_slot_info (struct slot *slot)
976 struct hotplug_slot_info *info;
979 info = kmalloc(sizeof(*info), GFP_KERNEL);
983 slot->hpc_ops->get_power_status(slot, &(info->power_status));
984 slot->hpc_ops->get_attention_status(slot, &(info->attention_status));
985 slot->hpc_ops->get_latch_status(slot, &(info->latch_status));
986 slot->hpc_ops->get_adapter_status(slot, &(info->adapter_status));
988 result = pci_hp_change_slot_info(slot->hotplug_slot, info);
993 static void interrupt_event_handler(struct controller *ctrl)
997 struct pci_func *func;
1000 struct slot *p_slot;
1002 dbg("%s:\n", __FUNCTION__);
1006 for (loop = 0; loop < 10; loop++) {
1007 if (ctrl->event_queue[loop].event_type != 0) {
1008 dbg("%s:loop %x event_type %x\n", __FUNCTION__, loop,
1009 ctrl->event_queue[loop].event_type);
1010 hp_slot = ctrl->event_queue[loop].hp_slot;
1012 func = shpchp_slot_find(ctrl->slot_bus, (hp_slot + ctrl->slot_device_offset), 0);
1014 p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
1016 dbg("%s: hp_slot %d, func %p, p_slot %p\n", __FUNCTION__, hp_slot, func, p_slot);
1018 if (ctrl->event_queue[loop].event_type == INT_BUTTON_CANCEL) {
1019 dbg("%s: button cancel\n", __FUNCTION__);
1020 del_timer(&p_slot->task_event);
1022 switch (p_slot->state) {
1023 case BLINKINGOFF_STATE:
1024 /* Wait for exclusive access to hardware */
1025 down(&ctrl->crit_sect);
1027 p_slot->hpc_ops->green_led_on(p_slot);
1028 /* Wait for the command to complete */
1029 wait_for_ctrl_irq (ctrl);
1031 p_slot->hpc_ops->set_attention_status(p_slot, 0);
1033 /* Wait for the command to complete */
1034 wait_for_ctrl_irq (ctrl);
1036 /* Done with exclusive hardware access */
1037 up(&ctrl->crit_sect);
1039 case BLINKINGON_STATE:
1040 /* Wait for exclusive access to hardware */
1041 down(&ctrl->crit_sect);
1043 p_slot->hpc_ops->green_led_off(p_slot);
1044 /* Wait for the command to complete */
1045 wait_for_ctrl_irq (ctrl);
1047 p_slot->hpc_ops->set_attention_status(p_slot, 0);
1048 /* Wait for the command to complete */
1049 wait_for_ctrl_irq (ctrl);
1051 /* Done with exclusive hardware access */
1052 up(&ctrl->crit_sect);
1056 warn("Not a valid state\n");
1059 info(msg_button_cancel, p_slot->number);
1060 p_slot->state = STATIC_STATE;
1061 } else if (ctrl->event_queue[loop].event_type == INT_BUTTON_PRESS) {
1062 /* Button Pressed (No action on 1st press...) */
1063 dbg("%s: Button pressed\n", __FUNCTION__);
1065 p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
1068 dbg("%s: slot is on\n", __FUNCTION__);
1069 p_slot->state = BLINKINGOFF_STATE;
1070 info(msg_button_off, p_slot->number);
1073 dbg("%s: slot is off\n", __FUNCTION__);
1074 p_slot->state = BLINKINGON_STATE;
1075 info(msg_button_on, p_slot->number);
1078 /* Wait for exclusive access to hardware */
1079 down(&ctrl->crit_sect);
1081 /* blink green LED and turn off amber */
1082 p_slot->hpc_ops->green_led_blink(p_slot);
1083 /* Wait for the command to complete */
1084 wait_for_ctrl_irq (ctrl);
1086 p_slot->hpc_ops->set_attention_status(p_slot, 0);
1088 /* Wait for the command to complete */
1089 wait_for_ctrl_irq (ctrl);
1091 /* Done with exclusive hardware access */
1092 up(&ctrl->crit_sect);
1094 init_timer(&p_slot->task_event);
1095 p_slot->task_event.expires = jiffies + 5 * HZ; /* 5 second delay */
1096 p_slot->task_event.function = (void (*)(unsigned long)) pushbutton_helper_thread;
1097 p_slot->task_event.data = (unsigned long) p_slot;
1099 dbg("%s: add_timer p_slot = %p\n", __FUNCTION__,(void *) p_slot);
1100 add_timer(&p_slot->task_event);
1101 } else if (ctrl->event_queue[loop].event_type == INT_POWER_FAULT) {
1102 /***********POWER FAULT********************/
1103 dbg("%s: power fault\n", __FUNCTION__);
1104 /* Wait for exclusive access to hardware */
1105 down(&ctrl->crit_sect);
1107 p_slot->hpc_ops->set_attention_status(p_slot, 1);
1108 /* Wait for the command to complete */
1109 wait_for_ctrl_irq (ctrl);
1111 p_slot->hpc_ops->green_led_off(p_slot);
1112 /* Wait for the command to complete */
1113 wait_for_ctrl_irq (ctrl);
1115 /* Done with exclusive hardware access */
1116 up(&ctrl->crit_sect);
1118 /* refresh notification */
1120 update_slot_info(p_slot);
1123 ctrl->event_queue[loop].event_type = 0;
1127 } /* End of FOR loop */
1134 int shpchp_enable_slot (struct slot *p_slot)
1138 struct pci_func *func;
1140 func = shpchp_slot_find(p_slot->bus, p_slot->device, 0);
1142 dbg("%s: Error! slot NULL\n", __FUNCTION__);
1146 /* Check to see if (latch closed, card present, power off) */
1147 down(&p_slot->ctrl->crit_sect);
1148 rc = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
1149 if (rc || !getstatus) {
1150 info("%s: no adapter on slot(%x)\n", __FUNCTION__, p_slot->number);
1151 up(&p_slot->ctrl->crit_sect);
1154 rc = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
1155 if (rc || getstatus) {
1156 info("%s: latch open on slot(%x)\n", __FUNCTION__, p_slot->number);
1157 up(&p_slot->ctrl->crit_sect);
1160 rc = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
1161 if (rc || getstatus) {
1162 info("%s: already enabled on slot(%x)\n", __FUNCTION__, p_slot->number);
1163 up(&p_slot->ctrl->crit_sect);
1166 up(&p_slot->ctrl->crit_sect);
1170 func = shpchp_slot_create(p_slot->bus);
1174 func->bus = p_slot->bus;
1175 func->device = p_slot->device;
1177 func->configured = 0;
1178 func->is_a_board = 1;
1180 /* We have to save the presence info for these slots */
1181 p_slot->hpc_ops->get_adapter_status(p_slot, &(func->presence_save));
1182 p_slot->hpc_ops->get_power_status(p_slot, &(func->pwr_save));
1183 dbg("%s: func->pwr_save %x\n", __FUNCTION__, func->pwr_save);
1184 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
1185 func->switch_save = !getstatus? 0x10:0;
1187 rc = board_added(func, p_slot->ctrl);
1189 if (is_bridge(func))
1190 bridge_slot_remove(func);
1194 /* Setup slot structure with entry for empty slot */
1195 func = shpchp_slot_create(p_slot->bus);
1197 return -ENOMEM; /* Out of memory */
1199 func->bus = p_slot->bus;
1200 func->device = p_slot->device;
1202 func->configured = 0;
1203 func->is_a_board = 1;
1205 /* We have to save the presence info for these slots */
1206 p_slot->hpc_ops->get_adapter_status(p_slot, &(func->presence_save));
1207 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
1208 func->switch_save = !getstatus? 0x10:0;
1212 update_slot_info(p_slot);
1218 int shpchp_disable_slot (struct slot *p_slot)
1220 u8 class_code, header_type, BCR;
1226 struct pci_bus *pci_bus;
1227 struct pci_func *func;
1232 pci_bus = p_slot->ctrl->pci_dev->subordinate;
1234 /* Check to see if (latch closed, card present, power on) */
1235 down(&p_slot->ctrl->crit_sect);
1237 ret = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
1238 if (ret || !getstatus) {
1239 info("%s: no adapter on slot(%x)\n", __FUNCTION__, p_slot->number);
1240 up(&p_slot->ctrl->crit_sect);
1243 ret = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
1244 if (ret || getstatus) {
1245 info("%s: latch open on slot(%x)\n", __FUNCTION__, p_slot->number);
1246 up(&p_slot->ctrl->crit_sect);
1249 ret = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
1250 if (ret || !getstatus) {
1251 info("%s: already disabled slot(%x)\n", __FUNCTION__, p_slot->number);
1252 up(&p_slot->ctrl->crit_sect);
1255 up(&p_slot->ctrl->crit_sect);
1257 func = shpchp_slot_find(p_slot->bus, p_slot->device, index++);
1259 /* Make sure there are no video controllers here
1260 * for all func of p_slot
1262 while (func && !rc) {
1263 pci_bus->number = func->bus;
1264 devfn = PCI_DEVFN(func->device, func->function);
1266 /* Check the Class Code */
1267 rc = pci_bus_read_config_byte (pci_bus, devfn, 0x0B, &class_code);
1271 if (class_code == PCI_BASE_CLASS_DISPLAY) {
1272 /* Display/Video adapter (not supported) */
1273 rc = REMOVE_NOT_SUPPORTED;
1275 /* See if it's a bridge */
1276 rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
1280 /* If it's a bridge, check the VGA Enable bit */
1281 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
1282 rc = pci_bus_read_config_byte (pci_bus, devfn, PCI_BRIDGE_CONTROL, &BCR);
1286 /* If the VGA Enable bit is set, remove isn't supported */
1287 if (BCR & PCI_BRIDGE_CTL_VGA) {
1288 rc = REMOVE_NOT_SUPPORTED;
1293 func = shpchp_slot_find(p_slot->bus, p_slot->device, index++);
1296 func = shpchp_slot_find(p_slot->bus, p_slot->device, 0);
1297 if ((func != NULL) && !rc) {
1298 rc = remove_board(func, p_slot->ctrl);
1303 update_slot_info(p_slot);