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 static inline char *slot_name(struct slot *p_slot)
48 return p_slot->hotplug_slot->name;
51 u8 pciehp_handle_attention_button(u8 hp_slot, void *inst_id)
53 struct controller *ctrl = (struct controller *) inst_id;
57 struct event_info *taskInfo;
59 /* Attention Button Change */
60 dbg("pciehp: Attention button interrupt received.\n");
62 /* This is the structure that tells the worker thread what to do */
63 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
64 p_slot = pciehp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
66 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
68 ctrl->next_event = (ctrl->next_event + 1) % MAX_EVENTS;
69 taskInfo->hp_slot = hp_slot;
74 * Button pressed - See if need to TAKE ACTION!!!
76 info("Button pressed on Slot(%s)\n", slot_name(p_slot));
77 taskInfo->event_type = INT_BUTTON_PRESS;
79 if ((p_slot->state == BLINKINGON_STATE)
80 || (p_slot->state == BLINKINGOFF_STATE)) {
81 /* Cancel if we are still blinking; this means that we press the
82 * attention again before the 5 sec. limit expires to cancel hot-add
85 taskInfo->event_type = INT_BUTTON_CANCEL;
86 info("Button cancel on Slot(%s)\n", slot_name(p_slot));
87 } else if ((p_slot->state == POWERON_STATE)
88 || (p_slot->state == POWEROFF_STATE)) {
89 /* Ignore if the slot is on power-on or power-off state; this
90 * means that the previous attention button action to hot-add or
91 * hot-remove is undergoing
93 taskInfo->event_type = INT_BUTTON_IGNORE;
94 info("Button ignore on Slot(%s)\n", slot_name(p_slot));
98 up(&event_semaphore); /* signal event thread that new event is posted */
104 u8 pciehp_handle_switch_change(u8 hp_slot, void *inst_id)
106 struct controller *ctrl = (struct controller *) inst_id;
110 struct event_info *taskInfo;
113 dbg("pciehp: Switch interrupt received.\n");
115 /* This is the structure that tells the worker thread
118 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
119 ctrl->next_event = (ctrl->next_event + 1) % MAX_EVENTS;
120 taskInfo->hp_slot = hp_slot;
123 p_slot = pciehp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
124 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
130 info("Latch open on Slot(%s)\n", slot_name(p_slot));
131 taskInfo->event_type = INT_SWITCH_OPEN;
136 info("Latch close on Slot(%s)\n", slot_name(p_slot));
137 taskInfo->event_type = INT_SWITCH_CLOSE;
141 up(&event_semaphore); /* signal event thread that new event is posted */
146 u8 pciehp_handle_presence_change(u8 hp_slot, void *inst_id)
148 struct controller *ctrl = (struct controller *) inst_id;
150 u8 presence_save, rc = 0;
151 struct event_info *taskInfo;
153 /* Presence Change */
154 dbg("pciehp: Presence/Notify input change.\n");
156 /* This is the structure that tells the worker thread
159 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
160 ctrl->next_event = (ctrl->next_event + 1) % MAX_EVENTS;
161 taskInfo->hp_slot = hp_slot;
164 p_slot = pciehp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
166 /* Switch is open, assume a presence change
167 * Save the presence state
169 p_slot->hpc_ops->get_adapter_status(p_slot, &presence_save);
174 info("Card present on Slot(%s)\n", slot_name(p_slot));
175 taskInfo->event_type = INT_PRESENCE_ON;
180 info("Card not present on Slot(%s)\n", slot_name(p_slot));
181 taskInfo->event_type = INT_PRESENCE_OFF;
185 up(&event_semaphore); /* signal event thread that new event is posted */
190 u8 pciehp_handle_power_fault(u8 hp_slot, void *inst_id)
192 struct controller *ctrl = (struct controller *) inst_id;
195 struct event_info *taskInfo;
198 dbg("pciehp: Power fault interrupt received.\n");
200 /* this is the structure that tells the worker thread
203 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
204 ctrl->next_event = (ctrl->next_event + 1) % MAX_EVENTS;
205 taskInfo->hp_slot = hp_slot;
208 p_slot = pciehp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
210 if ( !(p_slot->hpc_ops->query_power_fault(p_slot))) {
212 * power fault Cleared
214 info("Power fault cleared on Slot(%s)\n", slot_name(p_slot));
215 taskInfo->event_type = INT_POWER_FAULT_CLEAR;
220 info("Power fault on Slot(%s)\n", slot_name(p_slot));
221 taskInfo->event_type = INT_POWER_FAULT;
222 info("power fault bit %x set\n", hp_slot);
225 up(&event_semaphore); /* signal event thread that new event is posted */
230 /* The following routines constitute the bulk of the
231 hotplug controller logic
234 static void set_slot_off(struct controller *ctrl, struct slot * pslot)
236 /* Wait for exclusive access to hardware */
237 mutex_lock(&ctrl->ctrl_lock);
239 /* turn off slot, turn on Amber LED, turn off Green LED if supported*/
240 if (POWER_CTRL(ctrl->ctrlcap)) {
241 if (pslot->hpc_ops->power_off_slot(pslot)) {
242 err("%s: Issue of Slot Power Off command failed\n", __FUNCTION__);
243 mutex_unlock(&ctrl->ctrl_lock);
246 wait_for_ctrl_irq (ctrl);
249 if (PWR_LED(ctrl->ctrlcap)) {
250 pslot->hpc_ops->green_led_off(pslot);
251 wait_for_ctrl_irq (ctrl);
254 if (ATTN_LED(ctrl->ctrlcap)) {
255 if (pslot->hpc_ops->set_attention_status(pslot, 1)) {
256 err("%s: Issue of Set Attention Led command failed\n", __FUNCTION__);
257 mutex_unlock(&ctrl->ctrl_lock);
260 wait_for_ctrl_irq (ctrl);
263 /* Done with exclusive hardware access */
264 mutex_unlock(&ctrl->ctrl_lock);
268 * board_added - Called after a board has been added to the system.
270 * Turns power on for the board
274 static int board_added(struct slot *p_slot)
278 struct controller *ctrl = p_slot->ctrl;
280 hp_slot = p_slot->device - ctrl->slot_device_offset;
282 dbg("%s: slot device, slot offset, hp slot = %d, %d ,%d\n",
283 __FUNCTION__, p_slot->device,
284 ctrl->slot_device_offset, hp_slot);
286 /* Wait for exclusive access to hardware */
287 mutex_lock(&ctrl->ctrl_lock);
289 if (POWER_CTRL(ctrl->ctrlcap)) {
291 rc = p_slot->hpc_ops->power_on_slot(p_slot);
293 mutex_unlock(&ctrl->ctrl_lock);
297 /* Wait for the command to complete */
298 wait_for_ctrl_irq (ctrl);
301 if (PWR_LED(ctrl->ctrlcap)) {
302 p_slot->hpc_ops->green_led_blink(p_slot);
304 /* Wait for the command to complete */
305 wait_for_ctrl_irq (ctrl);
308 /* Done with exclusive hardware access */
309 mutex_unlock(&ctrl->ctrl_lock);
311 /* Wait for ~1 second */
312 wait_for_ctrl_irq (ctrl);
314 /* Check link training status */
315 rc = p_slot->hpc_ops->check_lnk_status(ctrl);
317 err("%s: Failed to check link status\n", __FUNCTION__);
318 set_slot_off(ctrl, p_slot);
322 /* Check for a power fault */
323 if (p_slot->hpc_ops->query_power_fault(p_slot)) {
324 dbg("%s: power fault detected\n", __FUNCTION__);
329 rc = pciehp_configure_device(p_slot);
331 err("Cannot add device 0x%x:%x\n", p_slot->bus,
337 * Some PCI Express root ports require fixup after hot-plug operation.
340 pci_fixup_device(pci_fixup_final, ctrl->pci_dev);
341 if (PWR_LED(ctrl->ctrlcap)) {
342 /* Wait for exclusive access to hardware */
343 mutex_lock(&ctrl->ctrl_lock);
345 p_slot->hpc_ops->green_led_on(p_slot);
347 /* Wait for the command to complete */
348 wait_for_ctrl_irq (ctrl);
350 /* Done with exclusive hardware access */
351 mutex_unlock(&ctrl->ctrl_lock);
356 set_slot_off(ctrl, p_slot);
362 * remove_board - Turns off slot and LED's
365 static int remove_board(struct slot *p_slot)
370 struct controller *ctrl = p_slot->ctrl;
372 if (pciehp_unconfigure_device(p_slot))
375 device = p_slot->device;
377 hp_slot = p_slot->device - ctrl->slot_device_offset;
378 p_slot = pciehp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
380 dbg("In %s, hp_slot = %d\n", __FUNCTION__, hp_slot);
382 /* Wait for exclusive access to hardware */
383 mutex_lock(&ctrl->ctrl_lock);
385 if (POWER_CTRL(ctrl->ctrlcap)) {
387 rc = p_slot->hpc_ops->power_off_slot(p_slot);
389 err("%s: Issue of Slot Disable command failed\n", __FUNCTION__);
390 mutex_unlock(&ctrl->ctrl_lock);
393 /* Wait for the command to complete */
394 wait_for_ctrl_irq (ctrl);
397 if (PWR_LED(ctrl->ctrlcap)) {
398 /* turn off Green LED */
399 p_slot->hpc_ops->green_led_off(p_slot);
401 /* Wait for the command to complete */
402 wait_for_ctrl_irq (ctrl);
405 /* Done with exclusive hardware access */
406 mutex_unlock(&ctrl->ctrl_lock);
412 static void pushbutton_helper_thread(unsigned long data)
414 pushbutton_pending = data;
416 up(&event_semaphore);
420 * pciehp_pushbutton_thread
422 * Scheduled procedure to handle blocking stuff for the pushbuttons
423 * Handles all pending events and exits.
426 static void pciehp_pushbutton_thread(unsigned long slot)
428 struct slot *p_slot = (struct slot *) slot;
431 pushbutton_pending = 0;
434 dbg("%s: Error! slot NULL\n", __FUNCTION__);
438 p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
440 p_slot->state = POWEROFF_STATE;
441 dbg("%s: disabling bus:device(%x:%x)\n", __FUNCTION__,
442 p_slot->bus, p_slot->device);
444 pciehp_disable_slot(p_slot);
445 p_slot->state = STATIC_STATE;
447 p_slot->state = POWERON_STATE;
448 dbg("%s: adding bus:device(%x:%x)\n", __FUNCTION__,
449 p_slot->bus, p_slot->device);
451 if (pciehp_enable_slot(p_slot) && PWR_LED(p_slot->ctrl->ctrlcap)) {
452 /* Wait for exclusive access to hardware */
453 mutex_lock(&p_slot->ctrl->ctrl_lock);
455 p_slot->hpc_ops->green_led_off(p_slot);
457 /* Wait for the command to complete */
458 wait_for_ctrl_irq (p_slot->ctrl);
460 /* Done with exclusive hardware access */
461 mutex_unlock(&p_slot->ctrl->ctrl_lock);
463 p_slot->state = STATIC_STATE;
470 * pciehp_surprise_rm_thread
472 * Scheduled procedure to handle blocking stuff for the surprise removal
473 * Handles all pending events and exits.
476 static void pciehp_surprise_rm_thread(unsigned long slot)
478 struct slot *p_slot = (struct slot *) slot;
481 surprise_rm_pending = 0;
484 dbg("%s: Error! slot NULL\n", __FUNCTION__);
488 p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
490 p_slot->state = POWEROFF_STATE;
491 dbg("%s: removing bus:device(%x:%x)\n",
492 __FUNCTION__, p_slot->bus, p_slot->device);
494 pciehp_disable_slot(p_slot);
495 p_slot->state = STATIC_STATE;
497 p_slot->state = POWERON_STATE;
498 dbg("%s: adding bus:device(%x:%x)\n",
499 __FUNCTION__, p_slot->bus, p_slot->device);
501 if (pciehp_enable_slot(p_slot) && PWR_LED(p_slot->ctrl->ctrlcap)) {
502 /* Wait for exclusive access to hardware */
503 mutex_lock(&p_slot->ctrl->ctrl_lock);
505 p_slot->hpc_ops->green_led_off(p_slot);
507 /* Wait for the command to complete */
508 wait_for_ctrl_irq (p_slot->ctrl);
510 /* Done with exclusive hardware access */
511 mutex_unlock(&p_slot->ctrl->ctrl_lock);
513 p_slot->state = STATIC_STATE;
521 /* this is the main worker thread */
522 static int event_thread(void* data)
524 struct controller *ctrl;
526 daemonize("pciehpd_event");
531 dbg("!!!!event_thread sleeping\n");
532 down_interruptible (&event_semaphore);
533 dbg("event_thread woken finished = %d\n", event_finished);
534 if (event_finished || signal_pending(current))
537 if (pushbutton_pending)
538 pciehp_pushbutton_thread(pushbutton_pending);
539 else if (surprise_rm_pending)
540 pciehp_surprise_rm_thread(surprise_rm_pending);
542 for (ctrl = pciehp_ctrl_list; ctrl; ctrl=ctrl->next)
543 interrupt_event_handler(ctrl);
545 dbg("event_thread signals exit\n");
550 int pciehp_event_start_thread(void)
554 /* initialize our semaphores */
555 init_MUTEX_LOCKED(&event_exit);
558 init_MUTEX_LOCKED(&event_semaphore);
559 pid = kernel_thread(event_thread, NULL, 0);
562 err ("Can't start up our event thread\n");
569 void pciehp_event_stop_thread(void)
572 up(&event_semaphore);
577 static int update_slot_info(struct slot *slot)
579 struct hotplug_slot_info *info;
580 /* char buffer[SLOT_NAME_SIZE]; */
583 info = kmalloc(sizeof(struct hotplug_slot_info), GFP_KERNEL);
587 /* make_slot_name (&buffer[0], SLOT_NAME_SIZE, slot); */
589 slot->hpc_ops->get_power_status(slot, &(info->power_status));
590 slot->hpc_ops->get_attention_status(slot, &(info->attention_status));
591 slot->hpc_ops->get_latch_status(slot, &(info->latch_status));
592 slot->hpc_ops->get_adapter_status(slot, &(info->adapter_status));
594 /* result = pci_hp_change_slot_info(buffer, info); */
595 result = pci_hp_change_slot_info(slot->hotplug_slot, info);
600 static void interrupt_event_handler(struct controller *ctrl)
611 for (loop = 0; loop < MAX_EVENTS; loop++) {
612 if (ctrl->event_queue[loop].event_type != 0) {
613 hp_slot = ctrl->event_queue[loop].hp_slot;
615 p_slot = pciehp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
617 if (ctrl->event_queue[loop].event_type == INT_BUTTON_CANCEL) {
618 dbg("button cancel\n");
619 del_timer(&p_slot->task_event);
621 switch (p_slot->state) {
622 case BLINKINGOFF_STATE:
623 /* Wait for exclusive access to hardware */
624 mutex_lock(&ctrl->ctrl_lock);
626 if (PWR_LED(ctrl->ctrlcap)) {
627 p_slot->hpc_ops->green_led_on(p_slot);
628 /* Wait for the command to complete */
629 wait_for_ctrl_irq (ctrl);
631 if (ATTN_LED(ctrl->ctrlcap)) {
632 p_slot->hpc_ops->set_attention_status(p_slot, 0);
634 /* Wait for the command to complete */
635 wait_for_ctrl_irq (ctrl);
637 /* Done with exclusive hardware access */
638 mutex_unlock(&ctrl->ctrl_lock);
640 case BLINKINGON_STATE:
641 /* Wait for exclusive access to hardware */
642 mutex_lock(&ctrl->ctrl_lock);
644 if (PWR_LED(ctrl->ctrlcap)) {
645 p_slot->hpc_ops->green_led_off(p_slot);
646 /* Wait for the command to complete */
647 wait_for_ctrl_irq (ctrl);
649 if (ATTN_LED(ctrl->ctrlcap)){
650 p_slot->hpc_ops->set_attention_status(p_slot, 0);
651 /* Wait for the command to complete */
652 wait_for_ctrl_irq (ctrl);
654 /* Done with exclusive hardware access */
655 mutex_unlock(&ctrl->ctrl_lock);
659 warn("Not a valid state\n");
662 info(msg_button_cancel, slot_name(p_slot));
663 p_slot->state = STATIC_STATE;
665 /* ***********Button Pressed (No action on 1st press...) */
666 else if (ctrl->event_queue[loop].event_type == INT_BUTTON_PRESS) {
668 if (ATTN_BUTTN(ctrl->ctrlcap)) {
669 dbg("Button pressed\n");
670 p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
674 p_slot->state = BLINKINGOFF_STATE;
675 info(msg_button_off, slot_name(p_slot));
678 dbg("slot is off\n");
679 p_slot->state = BLINKINGON_STATE;
680 info(msg_button_on, slot_name(p_slot));
683 /* Wait for exclusive access to hardware */
684 mutex_lock(&ctrl->ctrl_lock);
686 /* blink green LED and turn off amber */
687 if (PWR_LED(ctrl->ctrlcap)) {
688 p_slot->hpc_ops->green_led_blink(p_slot);
689 /* Wait for the command to complete */
690 wait_for_ctrl_irq (ctrl);
693 if (ATTN_LED(ctrl->ctrlcap)) {
694 p_slot->hpc_ops->set_attention_status(p_slot, 0);
696 /* Wait for the command to complete */
697 wait_for_ctrl_irq (ctrl);
700 /* Done with exclusive hardware access */
701 mutex_unlock(&ctrl->ctrl_lock);
703 init_timer(&p_slot->task_event);
704 p_slot->task_event.expires = jiffies + 5 * HZ; /* 5 second delay */
705 p_slot->task_event.function = (void (*)(unsigned long)) pushbutton_helper_thread;
706 p_slot->task_event.data = (unsigned long) p_slot;
708 add_timer(&p_slot->task_event);
711 /***********POWER FAULT********************/
712 else if (ctrl->event_queue[loop].event_type == INT_POWER_FAULT) {
713 if (POWER_CTRL(ctrl->ctrlcap)) {
714 dbg("power fault\n");
715 /* Wait for exclusive access to hardware */
716 mutex_lock(&ctrl->ctrl_lock);
718 if (ATTN_LED(ctrl->ctrlcap)) {
719 p_slot->hpc_ops->set_attention_status(p_slot, 1);
720 wait_for_ctrl_irq (ctrl);
723 if (PWR_LED(ctrl->ctrlcap)) {
724 p_slot->hpc_ops->green_led_off(p_slot);
725 wait_for_ctrl_irq (ctrl);
728 /* Done with exclusive hardware access */
729 mutex_unlock(&ctrl->ctrl_lock);
732 /***********SURPRISE REMOVAL********************/
733 else if ((ctrl->event_queue[loop].event_type == INT_PRESENCE_ON) ||
734 (ctrl->event_queue[loop].event_type == INT_PRESENCE_OFF)) {
735 if (HP_SUPR_RM(ctrl->ctrlcap)) {
736 dbg("Surprise Removal\n");
738 surprise_rm_pending = (unsigned long) p_slot;
739 up(&event_semaphore);
740 update_slot_info(p_slot);
744 /* refresh notification */
746 update_slot_info(p_slot);
749 ctrl->event_queue[loop].event_type = 0;
753 } /* End of FOR loop */
758 int pciehp_enable_slot(struct slot *p_slot)
763 /* Check to see if (latch closed, card present, power off) */
764 mutex_lock(&p_slot->ctrl->crit_sect);
766 rc = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
767 if (rc || !getstatus) {
768 info("%s: no adapter on slot(%s)\n", __FUNCTION__,
770 mutex_unlock(&p_slot->ctrl->crit_sect);
773 if (MRL_SENS(p_slot->ctrl->ctrlcap)) {
774 rc = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
775 if (rc || getstatus) {
776 info("%s: latch open on slot(%s)\n", __FUNCTION__,
778 mutex_unlock(&p_slot->ctrl->crit_sect);
783 if (POWER_CTRL(p_slot->ctrl->ctrlcap)) {
784 rc = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
785 if (rc || getstatus) {
786 info("%s: already enabled on slot(%s)\n", __FUNCTION__,
788 mutex_unlock(&p_slot->ctrl->crit_sect);
793 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
795 rc = board_added(p_slot);
797 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
800 update_slot_info(p_slot);
802 mutex_unlock(&p_slot->ctrl->crit_sect);
807 int pciehp_disable_slot(struct slot *p_slot)
815 /* Check to see if (latch closed, card present, power on) */
816 mutex_lock(&p_slot->ctrl->crit_sect);
818 if (!HP_SUPR_RM(p_slot->ctrl->ctrlcap)) {
819 ret = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
820 if (ret || !getstatus) {
821 info("%s: no adapter on slot(%s)\n", __FUNCTION__,
823 mutex_unlock(&p_slot->ctrl->crit_sect);
828 if (MRL_SENS(p_slot->ctrl->ctrlcap)) {
829 ret = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
830 if (ret || getstatus) {
831 info("%s: latch open on slot(%s)\n", __FUNCTION__,
833 mutex_unlock(&p_slot->ctrl->crit_sect);
838 if (POWER_CTRL(p_slot->ctrl->ctrlcap)) {
839 ret = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
840 if (ret || !getstatus) {
841 info("%s: already disabled slot(%s)\n", __FUNCTION__,
843 mutex_unlock(&p_slot->ctrl->crit_sect);
848 ret = remove_board(p_slot);
849 update_slot_info(p_slot);
851 mutex_unlock(&p_slot->ctrl->crit_sect);