2 * PCI Express 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/module.h>
31 #include <linux/kernel.h>
32 #include <linux/types.h>
33 #include <linux/smp_lock.h>
34 #include <linux/pci.h>
38 static void interrupt_event_handler(struct controller *ctrl);
40 static struct semaphore event_semaphore; /* mutex for process loop (up if something to process) */
41 static struct semaphore event_exit; /* guard ensure thread has exited before calling it quits */
42 static int event_finished;
43 static unsigned long pushbutton_pending; /* = 0 */
44 static unsigned long surprise_rm_pending; /* = 0 */
46 u8 pciehp_handle_attention_button(u8 hp_slot, void *inst_id)
48 struct controller *ctrl = (struct controller *) inst_id;
52 struct event_info *taskInfo;
54 /* Attention Button Change */
55 dbg("pciehp: Attention button interrupt received.\n");
57 /* This is the structure that tells the worker thread what to do */
58 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
59 p_slot = pciehp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
61 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
63 ctrl->next_event = (ctrl->next_event + 1) % MAX_EVENTS;
64 taskInfo->hp_slot = hp_slot;
69 * Button pressed - See if need to TAKE ACTION!!!
71 info("Button pressed on Slot(%d)\n", ctrl->first_slot + hp_slot);
72 taskInfo->event_type = INT_BUTTON_PRESS;
74 if ((p_slot->state == BLINKINGON_STATE)
75 || (p_slot->state == BLINKINGOFF_STATE)) {
76 /* Cancel if we are still blinking; this means that we press the
77 * attention again before the 5 sec. limit expires to cancel hot-add
80 taskInfo->event_type = INT_BUTTON_CANCEL;
81 info("Button cancel on Slot(%d)\n", ctrl->first_slot + hp_slot);
82 } else if ((p_slot->state == POWERON_STATE)
83 || (p_slot->state == POWEROFF_STATE)) {
84 /* Ignore if the slot is on power-on or power-off state; this
85 * means that the previous attention button action to hot-add or
86 * hot-remove is undergoing
88 taskInfo->event_type = INT_BUTTON_IGNORE;
89 info("Button ignore on Slot(%d)\n", ctrl->first_slot + hp_slot);
93 up(&event_semaphore); /* signal event thread that new event is posted */
99 u8 pciehp_handle_switch_change(u8 hp_slot, void *inst_id)
101 struct controller *ctrl = (struct controller *) inst_id;
105 struct event_info *taskInfo;
108 dbg("pciehp: Switch interrupt received.\n");
110 /* This is the structure that tells the worker thread
113 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
114 ctrl->next_event = (ctrl->next_event + 1) % MAX_EVENTS;
115 taskInfo->hp_slot = hp_slot;
118 p_slot = pciehp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
119 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
125 info("Latch open on Slot(%d)\n", ctrl->first_slot + hp_slot);
126 taskInfo->event_type = INT_SWITCH_OPEN;
131 info("Latch close on Slot(%d)\n", ctrl->first_slot + hp_slot);
132 taskInfo->event_type = INT_SWITCH_CLOSE;
136 up(&event_semaphore); /* signal event thread that new event is posted */
141 u8 pciehp_handle_presence_change(u8 hp_slot, void *inst_id)
143 struct controller *ctrl = (struct controller *) inst_id;
145 u8 presence_save, rc = 0;
146 struct event_info *taskInfo;
148 /* Presence Change */
149 dbg("pciehp: Presence/Notify input change.\n");
151 /* This is the structure that tells the worker thread
154 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
155 ctrl->next_event = (ctrl->next_event + 1) % MAX_EVENTS;
156 taskInfo->hp_slot = hp_slot;
159 p_slot = pciehp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
161 /* Switch is open, assume a presence change
162 * Save the presence state
164 p_slot->hpc_ops->get_adapter_status(p_slot, &presence_save);
169 info("Card present on Slot(%d)\n", ctrl->first_slot + hp_slot);
170 taskInfo->event_type = INT_PRESENCE_ON;
175 info("Card not present on Slot(%d)\n", ctrl->first_slot + hp_slot);
176 taskInfo->event_type = INT_PRESENCE_OFF;
180 up(&event_semaphore); /* signal event thread that new event is posted */
185 u8 pciehp_handle_power_fault(u8 hp_slot, void *inst_id)
187 struct controller *ctrl = (struct controller *) inst_id;
190 struct event_info *taskInfo;
193 dbg("pciehp: Power fault interrupt received.\n");
195 /* this is the structure that tells the worker thread
198 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
199 ctrl->next_event = (ctrl->next_event + 1) % MAX_EVENTS;
200 taskInfo->hp_slot = hp_slot;
203 p_slot = pciehp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
205 if ( !(p_slot->hpc_ops->query_power_fault(p_slot))) {
207 * power fault Cleared
209 info("Power fault cleared on Slot(%d)\n", ctrl->first_slot + hp_slot);
210 taskInfo->event_type = INT_POWER_FAULT_CLEAR;
215 info("Power fault on Slot(%d)\n", ctrl->first_slot + hp_slot);
216 taskInfo->event_type = INT_POWER_FAULT;
217 info("power fault bit %x set\n", hp_slot);
220 up(&event_semaphore); /* signal event thread that new event is posted */
225 /* The following routines constitute the bulk of the
226 hotplug controller logic
229 static void set_slot_off(struct controller *ctrl, struct slot * pslot)
231 /* Wait for exclusive access to hardware */
232 mutex_lock(&ctrl->crit_sect);
234 /* turn off slot, turn on Amber LED, turn off Green LED if supported*/
235 if (POWER_CTRL(ctrl->ctrlcap)) {
236 if (pslot->hpc_ops->power_off_slot(pslot)) {
237 err("%s: Issue of Slot Power Off command failed\n", __FUNCTION__);
238 mutex_unlock(&ctrl->crit_sect);
241 wait_for_ctrl_irq (ctrl);
244 if (PWR_LED(ctrl->ctrlcap)) {
245 pslot->hpc_ops->green_led_off(pslot);
246 wait_for_ctrl_irq (ctrl);
249 if (ATTN_LED(ctrl->ctrlcap)) {
250 if (pslot->hpc_ops->set_attention_status(pslot, 1)) {
251 err("%s: Issue of Set Attention Led command failed\n", __FUNCTION__);
252 mutex_unlock(&ctrl->crit_sect);
255 wait_for_ctrl_irq (ctrl);
258 /* Done with exclusive hardware access */
259 mutex_unlock(&ctrl->crit_sect);
263 * board_added - Called after a board has been added to the system.
265 * Turns power on for the board
269 static int board_added(struct slot *p_slot)
273 struct controller *ctrl = p_slot->ctrl;
275 hp_slot = p_slot->device - ctrl->slot_device_offset;
277 dbg("%s: slot device, slot offset, hp slot = %d, %d ,%d\n",
278 __FUNCTION__, p_slot->device,
279 ctrl->slot_device_offset, hp_slot);
281 /* Wait for exclusive access to hardware */
282 mutex_lock(&ctrl->crit_sect);
284 if (POWER_CTRL(ctrl->ctrlcap)) {
286 rc = p_slot->hpc_ops->power_on_slot(p_slot);
288 mutex_unlock(&ctrl->crit_sect);
292 /* Wait for the command to complete */
293 wait_for_ctrl_irq (ctrl);
296 if (PWR_LED(ctrl->ctrlcap)) {
297 p_slot->hpc_ops->green_led_blink(p_slot);
299 /* Wait for the command to complete */
300 wait_for_ctrl_irq (ctrl);
303 /* Done with exclusive hardware access */
304 mutex_unlock(&ctrl->crit_sect);
306 /* Wait for ~1 second */
307 wait_for_ctrl_irq (ctrl);
309 /* Check link training status */
310 rc = p_slot->hpc_ops->check_lnk_status(ctrl);
312 err("%s: Failed to check link status\n", __FUNCTION__);
313 set_slot_off(ctrl, p_slot);
317 /* Check for a power fault */
318 if (p_slot->hpc_ops->query_power_fault(p_slot)) {
319 dbg("%s: power fault detected\n", __FUNCTION__);
324 rc = pciehp_configure_device(p_slot);
326 err("Cannot add device 0x%x:%x\n", p_slot->bus,
332 * Some PCI Express root ports require fixup after hot-plug operation.
335 pci_fixup_device(pci_fixup_final, ctrl->pci_dev);
336 if (PWR_LED(ctrl->ctrlcap)) {
337 /* Wait for exclusive access to hardware */
338 mutex_lock(&ctrl->crit_sect);
340 p_slot->hpc_ops->green_led_on(p_slot);
342 /* Wait for the command to complete */
343 wait_for_ctrl_irq (ctrl);
345 /* Done with exclusive hardware access */
346 mutex_unlock(&ctrl->crit_sect);
351 set_slot_off(ctrl, p_slot);
357 * remove_board - Turns off slot and LED's
360 static int remove_board(struct slot *p_slot)
365 struct controller *ctrl = p_slot->ctrl;
367 if (pciehp_unconfigure_device(p_slot))
370 device = p_slot->device;
372 hp_slot = p_slot->device - ctrl->slot_device_offset;
373 p_slot = pciehp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
375 dbg("In %s, hp_slot = %d\n", __FUNCTION__, hp_slot);
377 /* Wait for exclusive access to hardware */
378 mutex_lock(&ctrl->crit_sect);
380 if (POWER_CTRL(ctrl->ctrlcap)) {
382 rc = p_slot->hpc_ops->power_off_slot(p_slot);
384 err("%s: Issue of Slot Disable command failed\n", __FUNCTION__);
385 mutex_unlock(&ctrl->crit_sect);
388 /* Wait for the command to complete */
389 wait_for_ctrl_irq (ctrl);
392 if (PWR_LED(ctrl->ctrlcap)) {
393 /* turn off Green LED */
394 p_slot->hpc_ops->green_led_off(p_slot);
396 /* Wait for the command to complete */
397 wait_for_ctrl_irq (ctrl);
400 /* Done with exclusive hardware access */
401 mutex_unlock(&ctrl->crit_sect);
407 static void pushbutton_helper_thread(unsigned long data)
409 pushbutton_pending = data;
411 up(&event_semaphore);
415 * pciehp_pushbutton_thread
417 * Scheduled procedure to handle blocking stuff for the pushbuttons
418 * Handles all pending events and exits.
421 static void pciehp_pushbutton_thread(unsigned long slot)
423 struct slot *p_slot = (struct slot *) slot;
426 pushbutton_pending = 0;
429 dbg("%s: Error! slot NULL\n", __FUNCTION__);
433 p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
435 p_slot->state = POWEROFF_STATE;
436 dbg("%s: disabling bus:device(%x:%x)\n", __FUNCTION__,
437 p_slot->bus, p_slot->device);
439 pciehp_disable_slot(p_slot);
440 p_slot->state = STATIC_STATE;
442 p_slot->state = POWERON_STATE;
443 dbg("%s: adding bus:device(%x:%x)\n", __FUNCTION__,
444 p_slot->bus, p_slot->device);
446 if (pciehp_enable_slot(p_slot) && PWR_LED(p_slot->ctrl->ctrlcap)) {
447 /* Wait for exclusive access to hardware */
448 mutex_lock(&p_slot->ctrl->crit_sect);
450 p_slot->hpc_ops->green_led_off(p_slot);
452 /* Wait for the command to complete */
453 wait_for_ctrl_irq (p_slot->ctrl);
455 /* Done with exclusive hardware access */
456 mutex_unlock(&p_slot->ctrl->crit_sect);
458 p_slot->state = STATIC_STATE;
465 * pciehp_surprise_rm_thread
467 * Scheduled procedure to handle blocking stuff for the surprise removal
468 * Handles all pending events and exits.
471 static void pciehp_surprise_rm_thread(unsigned long slot)
473 struct slot *p_slot = (struct slot *) slot;
476 surprise_rm_pending = 0;
479 dbg("%s: Error! slot NULL\n", __FUNCTION__);
483 p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
485 p_slot->state = POWEROFF_STATE;
486 dbg("%s: removing bus:device(%x:%x)\n",
487 __FUNCTION__, p_slot->bus, p_slot->device);
489 pciehp_disable_slot(p_slot);
490 p_slot->state = STATIC_STATE;
492 p_slot->state = POWERON_STATE;
493 dbg("%s: adding bus:device(%x:%x)\n",
494 __FUNCTION__, p_slot->bus, p_slot->device);
496 if (pciehp_enable_slot(p_slot) && PWR_LED(p_slot->ctrl->ctrlcap)) {
497 /* Wait for exclusive access to hardware */
498 mutex_lock(&p_slot->ctrl->crit_sect);
500 p_slot->hpc_ops->green_led_off(p_slot);
502 /* Wait for the command to complete */
503 wait_for_ctrl_irq (p_slot->ctrl);
505 /* Done with exclusive hardware access */
506 mutex_unlock(&p_slot->ctrl->crit_sect);
508 p_slot->state = STATIC_STATE;
516 /* this is the main worker thread */
517 static int event_thread(void* data)
519 struct controller *ctrl;
521 daemonize("pciehpd_event");
526 dbg("!!!!event_thread sleeping\n");
527 down_interruptible (&event_semaphore);
528 dbg("event_thread woken finished = %d\n", event_finished);
529 if (event_finished || signal_pending(current))
532 if (pushbutton_pending)
533 pciehp_pushbutton_thread(pushbutton_pending);
534 else if (surprise_rm_pending)
535 pciehp_surprise_rm_thread(surprise_rm_pending);
537 for (ctrl = pciehp_ctrl_list; ctrl; ctrl=ctrl->next)
538 interrupt_event_handler(ctrl);
540 dbg("event_thread signals exit\n");
545 int pciehp_event_start_thread(void)
549 /* initialize our semaphores */
550 init_MUTEX_LOCKED(&event_exit);
553 init_MUTEX_LOCKED(&event_semaphore);
554 pid = kernel_thread(event_thread, NULL, 0);
557 err ("Can't start up our event thread\n");
564 void pciehp_event_stop_thread(void)
567 up(&event_semaphore);
572 static int update_slot_info(struct slot *slot)
574 struct hotplug_slot_info *info;
575 /* char buffer[SLOT_NAME_SIZE]; */
578 info = kmalloc(sizeof(struct hotplug_slot_info), GFP_KERNEL);
582 /* make_slot_name (&buffer[0], SLOT_NAME_SIZE, slot); */
584 slot->hpc_ops->get_power_status(slot, &(info->power_status));
585 slot->hpc_ops->get_attention_status(slot, &(info->attention_status));
586 slot->hpc_ops->get_latch_status(slot, &(info->latch_status));
587 slot->hpc_ops->get_adapter_status(slot, &(info->adapter_status));
589 /* result = pci_hp_change_slot_info(buffer, info); */
590 result = pci_hp_change_slot_info(slot->hotplug_slot, info);
595 static void interrupt_event_handler(struct controller *ctrl)
606 for (loop = 0; loop < MAX_EVENTS; loop++) {
607 if (ctrl->event_queue[loop].event_type != 0) {
608 hp_slot = ctrl->event_queue[loop].hp_slot;
610 p_slot = pciehp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
612 if (ctrl->event_queue[loop].event_type == INT_BUTTON_CANCEL) {
613 dbg("button cancel\n");
614 del_timer(&p_slot->task_event);
616 switch (p_slot->state) {
617 case BLINKINGOFF_STATE:
618 /* Wait for exclusive access to hardware */
619 mutex_lock(&ctrl->crit_sect);
621 if (PWR_LED(ctrl->ctrlcap)) {
622 p_slot->hpc_ops->green_led_on(p_slot);
623 /* Wait for the command to complete */
624 wait_for_ctrl_irq (ctrl);
626 if (ATTN_LED(ctrl->ctrlcap)) {
627 p_slot->hpc_ops->set_attention_status(p_slot, 0);
629 /* Wait for the command to complete */
630 wait_for_ctrl_irq (ctrl);
632 /* Done with exclusive hardware access */
633 mutex_unlock(&ctrl->crit_sect);
635 case BLINKINGON_STATE:
636 /* Wait for exclusive access to hardware */
637 mutex_lock(&ctrl->crit_sect);
639 if (PWR_LED(ctrl->ctrlcap)) {
640 p_slot->hpc_ops->green_led_off(p_slot);
641 /* Wait for the command to complete */
642 wait_for_ctrl_irq (ctrl);
644 if (ATTN_LED(ctrl->ctrlcap)){
645 p_slot->hpc_ops->set_attention_status(p_slot, 0);
646 /* Wait for the command to complete */
647 wait_for_ctrl_irq (ctrl);
649 /* Done with exclusive hardware access */
650 mutex_unlock(&ctrl->crit_sect);
654 warn("Not a valid state\n");
657 info(msg_button_cancel, p_slot->number);
658 p_slot->state = STATIC_STATE;
660 /* ***********Button Pressed (No action on 1st press...) */
661 else if (ctrl->event_queue[loop].event_type == INT_BUTTON_PRESS) {
663 if (ATTN_BUTTN(ctrl->ctrlcap)) {
664 dbg("Button pressed\n");
665 p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
669 p_slot->state = BLINKINGOFF_STATE;
670 info(msg_button_off, p_slot->number);
673 dbg("slot is off\n");
674 p_slot->state = BLINKINGON_STATE;
675 info(msg_button_on, p_slot->number);
678 /* Wait for exclusive access to hardware */
679 mutex_lock(&ctrl->crit_sect);
681 /* blink green LED and turn off amber */
682 if (PWR_LED(ctrl->ctrlcap)) {
683 p_slot->hpc_ops->green_led_blink(p_slot);
684 /* Wait for the command to complete */
685 wait_for_ctrl_irq (ctrl);
688 if (ATTN_LED(ctrl->ctrlcap)) {
689 p_slot->hpc_ops->set_attention_status(p_slot, 0);
691 /* Wait for the command to complete */
692 wait_for_ctrl_irq (ctrl);
695 /* Done with exclusive hardware access */
696 mutex_unlock(&ctrl->crit_sect);
698 init_timer(&p_slot->task_event);
699 p_slot->task_event.expires = jiffies + 5 * HZ; /* 5 second delay */
700 p_slot->task_event.function = (void (*)(unsigned long)) pushbutton_helper_thread;
701 p_slot->task_event.data = (unsigned long) p_slot;
703 add_timer(&p_slot->task_event);
706 /***********POWER FAULT********************/
707 else if (ctrl->event_queue[loop].event_type == INT_POWER_FAULT) {
708 if (POWER_CTRL(ctrl->ctrlcap)) {
709 dbg("power fault\n");
710 /* Wait for exclusive access to hardware */
711 mutex_lock(&ctrl->crit_sect);
713 if (ATTN_LED(ctrl->ctrlcap)) {
714 p_slot->hpc_ops->set_attention_status(p_slot, 1);
715 wait_for_ctrl_irq (ctrl);
718 if (PWR_LED(ctrl->ctrlcap)) {
719 p_slot->hpc_ops->green_led_off(p_slot);
720 wait_for_ctrl_irq (ctrl);
723 /* Done with exclusive hardware access */
724 mutex_unlock(&ctrl->crit_sect);
727 /***********SURPRISE REMOVAL********************/
728 else if ((ctrl->event_queue[loop].event_type == INT_PRESENCE_ON) ||
729 (ctrl->event_queue[loop].event_type == INT_PRESENCE_OFF)) {
730 if (HP_SUPR_RM(ctrl->ctrlcap)) {
731 dbg("Surprise Removal\n");
733 surprise_rm_pending = (unsigned long) p_slot;
734 up(&event_semaphore);
735 update_slot_info(p_slot);
739 /* refresh notification */
741 update_slot_info(p_slot);
744 ctrl->event_queue[loop].event_type = 0;
748 } /* End of FOR loop */
753 int pciehp_enable_slot(struct slot *p_slot)
758 /* Check to see if (latch closed, card present, power off) */
759 mutex_lock(&p_slot->ctrl->crit_sect);
761 rc = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
762 if (rc || !getstatus) {
763 info("%s: no adapter on slot(%x)\n", __FUNCTION__, p_slot->number);
764 mutex_unlock(&p_slot->ctrl->crit_sect);
767 if (MRL_SENS(p_slot->ctrl->ctrlcap)) {
768 rc = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
769 if (rc || getstatus) {
770 info("%s: latch open on slot(%x)\n", __FUNCTION__, p_slot->number);
771 mutex_unlock(&p_slot->ctrl->crit_sect);
776 if (POWER_CTRL(p_slot->ctrl->ctrlcap)) {
777 rc = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
778 if (rc || getstatus) {
779 info("%s: already enabled on slot(%x)\n", __FUNCTION__, p_slot->number);
780 mutex_unlock(&p_slot->ctrl->crit_sect);
784 mutex_unlock(&p_slot->ctrl->crit_sect);
786 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
788 rc = board_added(p_slot);
790 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
794 update_slot_info(p_slot);
800 int pciehp_disable_slot(struct slot *p_slot)
808 /* Check to see if (latch closed, card present, power on) */
809 mutex_lock(&p_slot->ctrl->crit_sect);
811 if (!HP_SUPR_RM(p_slot->ctrl->ctrlcap)) {
812 ret = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
813 if (ret || !getstatus) {
814 info("%s: no adapter on slot(%x)\n", __FUNCTION__, p_slot->number);
815 mutex_unlock(&p_slot->ctrl->crit_sect);
820 if (MRL_SENS(p_slot->ctrl->ctrlcap)) {
821 ret = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
822 if (ret || getstatus) {
823 info("%s: latch open on slot(%x)\n", __FUNCTION__, p_slot->number);
824 mutex_unlock(&p_slot->ctrl->crit_sect);
829 if (POWER_CTRL(p_slot->ctrl->ctrlcap)) {
830 ret = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
831 if (ret || !getstatus) {
832 info("%s: already disabled slot(%x)\n", __FUNCTION__, p_slot->number);
833 mutex_unlock(&p_slot->ctrl->crit_sect);
838 mutex_unlock(&p_slot->ctrl->crit_sect);
840 ret = remove_board(p_slot);
841 update_slot_info(p_slot);