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>
35 #include <linux/workqueue.h>
39 static void interrupt_event_handler(struct work_struct *work);
41 static int queue_interrupt_event(struct slot *p_slot, u32 event_type)
43 struct event_info *info;
45 info = kmalloc(sizeof(*info), GFP_ATOMIC);
49 info->event_type = event_type;
50 info->p_slot = p_slot;
51 INIT_WORK(&info->work, interrupt_event_handler);
53 schedule_work(&info->work);
58 u8 pciehp_handle_attention_button(u8 hp_slot, struct controller *ctrl)
63 /* Attention Button Change */
64 dbg("pciehp: Attention button interrupt received.\n");
66 p_slot = pciehp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
69 * Button pressed - See if need to TAKE ACTION!!!
71 info("Button pressed on Slot(%s)\n", p_slot->name);
72 event_type = INT_BUTTON_PRESS;
74 queue_interrupt_event(p_slot, event_type);
79 u8 pciehp_handle_switch_change(u8 hp_slot, struct controller *ctrl)
86 dbg("pciehp: Switch interrupt received.\n");
88 p_slot = pciehp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
89 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
95 info("Latch open on Slot(%s)\n", p_slot->name);
96 event_type = INT_SWITCH_OPEN;
101 info("Latch close on Slot(%s)\n", p_slot->name);
102 event_type = INT_SWITCH_CLOSE;
105 queue_interrupt_event(p_slot, event_type);
110 u8 pciehp_handle_presence_change(u8 hp_slot, struct controller *ctrl)
116 /* Presence Change */
117 dbg("pciehp: Presence/Notify input change.\n");
119 p_slot = pciehp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
121 /* Switch is open, assume a presence change
122 * Save the presence state
124 p_slot->hpc_ops->get_adapter_status(p_slot, &presence_save);
129 info("Card present on Slot(%s)\n", p_slot->name);
130 event_type = INT_PRESENCE_ON;
135 info("Card not present on Slot(%s)\n", p_slot->name);
136 event_type = INT_PRESENCE_OFF;
139 queue_interrupt_event(p_slot, event_type);
144 u8 pciehp_handle_power_fault(u8 hp_slot, struct controller *ctrl)
150 dbg("pciehp: Power fault interrupt received.\n");
152 p_slot = pciehp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
154 if ( !(p_slot->hpc_ops->query_power_fault(p_slot))) {
156 * power fault Cleared
158 info("Power fault cleared on Slot(%s)\n", p_slot->name);
159 event_type = INT_POWER_FAULT_CLEAR;
164 info("Power fault on Slot(%s)\n", p_slot->name);
165 event_type = INT_POWER_FAULT;
166 info("power fault bit %x set\n", hp_slot);
169 queue_interrupt_event(p_slot, event_type);
174 /* The following routines constitute the bulk of the
175 hotplug controller logic
178 static void set_slot_off(struct controller *ctrl, struct slot * pslot)
180 /* turn off slot, turn on Amber LED, turn off Green LED if supported*/
181 if (POWER_CTRL(ctrl->ctrlcap)) {
182 if (pslot->hpc_ops->power_off_slot(pslot)) {
183 err("%s: Issue of Slot Power Off command failed\n",
189 if (PWR_LED(ctrl->ctrlcap))
190 pslot->hpc_ops->green_led_off(pslot);
192 if (ATTN_LED(ctrl->ctrlcap)) {
193 if (pslot->hpc_ops->set_attention_status(pslot, 1)) {
194 err("%s: Issue of Set Attention Led command failed\n",
199 * After turning power off, we must wait for at least
200 * 1 second before taking any action that relies on
201 * power having been removed from the slot/adapter.
208 * board_added - Called after a board has been added to the system.
209 * @p_slot: &slot where board is added
211 * Turns power on for the board.
214 static int board_added(struct slot *p_slot)
217 struct controller *ctrl = p_slot->ctrl;
219 dbg("%s: slot device, slot offset, hp slot = %d, %d ,%d\n",
220 __FUNCTION__, p_slot->device,
221 ctrl->slot_device_offset, p_slot->hp_slot);
223 if (POWER_CTRL(ctrl->ctrlcap)) {
225 retval = p_slot->hpc_ops->power_on_slot(p_slot);
230 if (PWR_LED(ctrl->ctrlcap))
231 p_slot->hpc_ops->green_led_blink(p_slot);
233 /* Wait for ~1 second */
236 /* Check link training status */
237 retval = p_slot->hpc_ops->check_lnk_status(ctrl);
239 err("%s: Failed to check link status\n", __FUNCTION__);
240 set_slot_off(ctrl, p_slot);
244 /* Check for a power fault */
245 if (p_slot->hpc_ops->query_power_fault(p_slot)) {
246 dbg("%s: power fault detected\n", __FUNCTION__);
247 retval = POWER_FAILURE;
251 retval = pciehp_configure_device(p_slot);
253 err("Cannot add device 0x%x:%x\n", p_slot->bus,
259 * Some PCI Express root ports require fixup after hot-plug operation.
262 pci_fixup_device(pci_fixup_final, ctrl->pci_dev);
263 if (PWR_LED(ctrl->ctrlcap))
264 p_slot->hpc_ops->green_led_on(p_slot);
269 set_slot_off(ctrl, p_slot);
274 * remove_board - Turns off slot and LEDs
275 * @p_slot: slot where board is being removed
277 static int remove_board(struct slot *p_slot)
280 struct controller *ctrl = p_slot->ctrl;
282 retval = pciehp_unconfigure_device(p_slot);
286 dbg("In %s, hp_slot = %d\n", __FUNCTION__, p_slot->hp_slot);
288 if (POWER_CTRL(ctrl->ctrlcap)) {
290 retval = p_slot->hpc_ops->power_off_slot(p_slot);
292 err("%s: Issue of Slot Disable command failed\n",
298 if (PWR_LED(ctrl->ctrlcap))
299 /* turn off Green LED */
300 p_slot->hpc_ops->green_led_off(p_slot);
305 struct power_work_info {
307 struct work_struct work;
311 * pciehp_power_thread - handle pushbutton events
312 * @work: &struct work_struct describing work to be done
314 * Scheduled procedure to handle blocking stuff for the pushbuttons.
315 * Handles all pending events and exits.
317 static void pciehp_power_thread(struct work_struct *work)
319 struct power_work_info *info =
320 container_of(work, struct power_work_info, work);
321 struct slot *p_slot = info->p_slot;
323 mutex_lock(&p_slot->lock);
324 switch (p_slot->state) {
326 mutex_unlock(&p_slot->lock);
327 dbg("%s: disabling bus:device(%x:%x)\n",
328 __FUNCTION__, p_slot->bus, p_slot->device);
329 pciehp_disable_slot(p_slot);
330 mutex_lock(&p_slot->lock);
331 p_slot->state = STATIC_STATE;
334 mutex_unlock(&p_slot->lock);
335 if (pciehp_enable_slot(p_slot) &&
336 PWR_LED(p_slot->ctrl->ctrlcap))
337 p_slot->hpc_ops->green_led_off(p_slot);
338 mutex_lock(&p_slot->lock);
339 p_slot->state = STATIC_STATE;
344 mutex_unlock(&p_slot->lock);
349 void pciehp_queue_pushbutton_work(struct work_struct *work)
351 struct slot *p_slot = container_of(work, struct slot, work.work);
352 struct power_work_info *info;
354 info = kmalloc(sizeof(*info), GFP_KERNEL);
356 err("%s: Cannot allocate memory\n", __FUNCTION__);
359 info->p_slot = p_slot;
360 INIT_WORK(&info->work, pciehp_power_thread);
362 mutex_lock(&p_slot->lock);
363 switch (p_slot->state) {
364 case BLINKINGOFF_STATE:
365 p_slot->state = POWEROFF_STATE;
367 case BLINKINGON_STATE:
368 p_slot->state = POWERON_STATE;
373 queue_work(pciehp_wq, &info->work);
375 mutex_unlock(&p_slot->lock);
378 static int update_slot_info(struct slot *slot)
380 struct hotplug_slot_info *info;
383 info = kmalloc(sizeof(*info), GFP_KERNEL);
387 slot->hpc_ops->get_power_status(slot, &(info->power_status));
388 slot->hpc_ops->get_attention_status(slot, &(info->attention_status));
389 slot->hpc_ops->get_latch_status(slot, &(info->latch_status));
390 slot->hpc_ops->get_adapter_status(slot, &(info->adapter_status));
392 result = pci_hp_change_slot_info(slot->hotplug_slot, info);
398 * Note: This function must be called with slot->lock held
400 static void handle_button_press_event(struct slot *p_slot)
402 struct controller *ctrl = p_slot->ctrl;
405 switch (p_slot->state) {
407 p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
409 p_slot->state = BLINKINGOFF_STATE;
410 info("PCI slot #%s - powering off due to button "
411 "press.\n", p_slot->name);
413 p_slot->state = BLINKINGON_STATE;
414 info("PCI slot #%s - powering on due to button "
415 "press.\n", p_slot->name);
417 /* blink green LED and turn off amber */
418 if (PWR_LED(ctrl->ctrlcap))
419 p_slot->hpc_ops->green_led_blink(p_slot);
420 if (ATTN_LED(ctrl->ctrlcap))
421 p_slot->hpc_ops->set_attention_status(p_slot, 0);
423 schedule_delayed_work(&p_slot->work, 5*HZ);
425 case BLINKINGOFF_STATE:
426 case BLINKINGON_STATE:
428 * Cancel if we are still blinking; this means that we
429 * press the attention again before the 5 sec. limit
430 * expires to cancel hot-add or hot-remove
432 info("Button cancel on Slot(%s)\n", p_slot->name);
433 dbg("%s: button cancel\n", __FUNCTION__);
434 cancel_delayed_work(&p_slot->work);
435 if (p_slot->state == BLINKINGOFF_STATE) {
436 if (PWR_LED(ctrl->ctrlcap))
437 p_slot->hpc_ops->green_led_on(p_slot);
439 if (PWR_LED(ctrl->ctrlcap))
440 p_slot->hpc_ops->green_led_off(p_slot);
442 if (ATTN_LED(ctrl->ctrlcap))
443 p_slot->hpc_ops->set_attention_status(p_slot, 0);
444 info("PCI slot #%s - action canceled due to button press\n",
446 p_slot->state = STATIC_STATE;
451 * Ignore if the slot is on power-on or power-off state;
452 * this means that the previous attention button action
453 * to hot-add or hot-remove is undergoing
455 info("Button ignore on Slot(%s)\n", p_slot->name);
456 update_slot_info(p_slot);
459 warn("Not a valid state\n");
465 * Note: This function must be called with slot->lock held
467 static void handle_surprise_event(struct slot *p_slot)
470 struct power_work_info *info;
472 info = kmalloc(sizeof(*info), GFP_KERNEL);
474 err("%s: Cannot allocate memory\n", __FUNCTION__);
477 info->p_slot = p_slot;
478 INIT_WORK(&info->work, pciehp_power_thread);
480 p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
482 p_slot->state = POWEROFF_STATE;
484 p_slot->state = POWERON_STATE;
486 queue_work(pciehp_wq, &info->work);
489 static void interrupt_event_handler(struct work_struct *work)
491 struct event_info *info = container_of(work, struct event_info, work);
492 struct slot *p_slot = info->p_slot;
493 struct controller *ctrl = p_slot->ctrl;
495 mutex_lock(&p_slot->lock);
496 switch (info->event_type) {
497 case INT_BUTTON_PRESS:
498 handle_button_press_event(p_slot);
500 case INT_POWER_FAULT:
501 if (!POWER_CTRL(ctrl->ctrlcap))
503 if (ATTN_LED(ctrl->ctrlcap))
504 p_slot->hpc_ops->set_attention_status(p_slot, 1);
505 if (PWR_LED(ctrl->ctrlcap))
506 p_slot->hpc_ops->green_led_off(p_slot);
508 case INT_PRESENCE_ON:
509 case INT_PRESENCE_OFF:
510 if (!HP_SUPR_RM(ctrl->ctrlcap))
512 dbg("Surprise Removal\n");
513 update_slot_info(p_slot);
514 handle_surprise_event(p_slot);
517 update_slot_info(p_slot);
520 mutex_unlock(&p_slot->lock);
525 int pciehp_enable_slot(struct slot *p_slot)
530 /* Check to see if (latch closed, card present, power off) */
531 mutex_lock(&p_slot->ctrl->crit_sect);
533 rc = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
534 if (rc || !getstatus) {
535 info("%s: no adapter on slot(%s)\n", __FUNCTION__,
537 mutex_unlock(&p_slot->ctrl->crit_sect);
540 if (MRL_SENS(p_slot->ctrl->ctrlcap)) {
541 rc = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
542 if (rc || getstatus) {
543 info("%s: latch open on slot(%s)\n", __FUNCTION__,
545 mutex_unlock(&p_slot->ctrl->crit_sect);
550 if (POWER_CTRL(p_slot->ctrl->ctrlcap)) {
551 rc = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
552 if (rc || getstatus) {
553 info("%s: already enabled on slot(%s)\n", __FUNCTION__,
555 mutex_unlock(&p_slot->ctrl->crit_sect);
560 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
562 rc = board_added(p_slot);
564 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
567 update_slot_info(p_slot);
569 mutex_unlock(&p_slot->ctrl->crit_sect);
574 int pciehp_disable_slot(struct slot *p_slot)
582 /* Check to see if (latch closed, card present, power on) */
583 mutex_lock(&p_slot->ctrl->crit_sect);
585 if (!HP_SUPR_RM(p_slot->ctrl->ctrlcap)) {
586 ret = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
587 if (ret || !getstatus) {
588 info("%s: no adapter on slot(%s)\n", __FUNCTION__,
590 mutex_unlock(&p_slot->ctrl->crit_sect);
595 if (MRL_SENS(p_slot->ctrl->ctrlcap)) {
596 ret = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
597 if (ret || getstatus) {
598 info("%s: latch open on slot(%s)\n", __FUNCTION__,
600 mutex_unlock(&p_slot->ctrl->crit_sect);
605 if (POWER_CTRL(p_slot->ctrl->ctrlcap)) {
606 ret = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
607 if (ret || !getstatus) {
608 info("%s: already disabled slot(%s)\n", __FUNCTION__,
610 mutex_unlock(&p_slot->ctrl->crit_sect);
614 * After turning power off, we must wait for at least
615 * 1 second before taking any action that relies on
616 * power having been removed from the slot/adapter.
621 ret = remove_board(p_slot);
622 update_slot_info(p_slot);
624 mutex_unlock(&p_slot->ctrl->crit_sect);
628 int pciehp_sysfs_enable_slot(struct slot *p_slot)
630 int retval = -ENODEV;
632 mutex_lock(&p_slot->lock);
633 switch (p_slot->state) {
634 case BLINKINGON_STATE:
635 cancel_delayed_work(&p_slot->work);
637 p_slot->state = POWERON_STATE;
638 mutex_unlock(&p_slot->lock);
639 retval = pciehp_enable_slot(p_slot);
640 mutex_lock(&p_slot->lock);
641 p_slot->state = STATIC_STATE;
644 info("Slot %s is already in powering on state\n",
647 case BLINKINGOFF_STATE:
649 info("Already enabled on slot %s\n", p_slot->name);
652 err("Not a valid state on slot %s\n", p_slot->name);
655 mutex_unlock(&p_slot->lock);
660 int pciehp_sysfs_disable_slot(struct slot *p_slot)
662 int retval = -ENODEV;
664 mutex_lock(&p_slot->lock);
665 switch (p_slot->state) {
666 case BLINKINGOFF_STATE:
667 cancel_delayed_work(&p_slot->work);
669 p_slot->state = POWEROFF_STATE;
670 mutex_unlock(&p_slot->lock);
671 retval = pciehp_disable_slot(p_slot);
672 mutex_lock(&p_slot->lock);
673 p_slot->state = STATIC_STATE;
676 info("Slot %s is already in powering off state\n",
679 case BLINKINGON_STATE:
681 info("Already disabled on slot %s\n", p_slot->name);
684 err("Not a valid state on slot %s\n", p_slot->name);
687 mutex_unlock(&p_slot->lock);