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)
218 struct controller *ctrl = p_slot->ctrl;
220 hp_slot = p_slot->device - ctrl->slot_device_offset;
222 dbg("%s: slot device, slot offset, hp slot = %d, %d ,%d\n",
223 __FUNCTION__, p_slot->device,
224 ctrl->slot_device_offset, hp_slot);
226 if (POWER_CTRL(ctrl->ctrlcap)) {
228 retval = p_slot->hpc_ops->power_on_slot(p_slot);
233 if (PWR_LED(ctrl->ctrlcap))
234 p_slot->hpc_ops->green_led_blink(p_slot);
236 /* Wait for ~1 second */
239 /* Check link training status */
240 retval = p_slot->hpc_ops->check_lnk_status(ctrl);
242 err("%s: Failed to check link status\n", __FUNCTION__);
243 set_slot_off(ctrl, p_slot);
247 /* Check for a power fault */
248 if (p_slot->hpc_ops->query_power_fault(p_slot)) {
249 dbg("%s: power fault detected\n", __FUNCTION__);
250 retval = POWER_FAILURE;
254 retval = pciehp_configure_device(p_slot);
256 err("Cannot add device 0x%x:%x\n", p_slot->bus,
262 * Some PCI Express root ports require fixup after hot-plug operation.
265 pci_fixup_device(pci_fixup_final, ctrl->pci_dev);
266 if (PWR_LED(ctrl->ctrlcap))
267 p_slot->hpc_ops->green_led_on(p_slot);
272 set_slot_off(ctrl, p_slot);
277 * remove_board - Turns off slot and LEDs
278 * @p_slot: slot where board is being removed
280 static int remove_board(struct slot *p_slot)
285 struct controller *ctrl = p_slot->ctrl;
287 retval = pciehp_unconfigure_device(p_slot);
291 device = p_slot->device;
292 hp_slot = p_slot->device - ctrl->slot_device_offset;
293 p_slot = pciehp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
295 dbg("In %s, hp_slot = %d\n", __FUNCTION__, hp_slot);
297 if (POWER_CTRL(ctrl->ctrlcap)) {
299 retval = p_slot->hpc_ops->power_off_slot(p_slot);
301 err("%s: Issue of Slot Disable command failed\n",
307 if (PWR_LED(ctrl->ctrlcap))
308 /* turn off Green LED */
309 p_slot->hpc_ops->green_led_off(p_slot);
314 struct power_work_info {
316 struct work_struct work;
320 * pciehp_power_thread - handle pushbutton events
321 * @work: &struct work_struct describing work to be done
323 * Scheduled procedure to handle blocking stuff for the pushbuttons.
324 * Handles all pending events and exits.
326 static void pciehp_power_thread(struct work_struct *work)
328 struct power_work_info *info =
329 container_of(work, struct power_work_info, work);
330 struct slot *p_slot = info->p_slot;
332 mutex_lock(&p_slot->lock);
333 switch (p_slot->state) {
335 mutex_unlock(&p_slot->lock);
336 dbg("%s: disabling bus:device(%x:%x)\n",
337 __FUNCTION__, p_slot->bus, p_slot->device);
338 pciehp_disable_slot(p_slot);
339 mutex_lock(&p_slot->lock);
340 p_slot->state = STATIC_STATE;
343 mutex_unlock(&p_slot->lock);
344 if (pciehp_enable_slot(p_slot) &&
345 PWR_LED(p_slot->ctrl->ctrlcap))
346 p_slot->hpc_ops->green_led_off(p_slot);
347 mutex_lock(&p_slot->lock);
348 p_slot->state = STATIC_STATE;
353 mutex_unlock(&p_slot->lock);
358 void pciehp_queue_pushbutton_work(struct work_struct *work)
360 struct slot *p_slot = container_of(work, struct slot, work.work);
361 struct power_work_info *info;
363 info = kmalloc(sizeof(*info), GFP_KERNEL);
365 err("%s: Cannot allocate memory\n", __FUNCTION__);
368 info->p_slot = p_slot;
369 INIT_WORK(&info->work, pciehp_power_thread);
371 mutex_lock(&p_slot->lock);
372 switch (p_slot->state) {
373 case BLINKINGOFF_STATE:
374 p_slot->state = POWEROFF_STATE;
376 case BLINKINGON_STATE:
377 p_slot->state = POWERON_STATE;
382 queue_work(pciehp_wq, &info->work);
384 mutex_unlock(&p_slot->lock);
387 static int update_slot_info(struct slot *slot)
389 struct hotplug_slot_info *info;
392 info = kmalloc(sizeof(*info), GFP_KERNEL);
396 slot->hpc_ops->get_power_status(slot, &(info->power_status));
397 slot->hpc_ops->get_attention_status(slot, &(info->attention_status));
398 slot->hpc_ops->get_latch_status(slot, &(info->latch_status));
399 slot->hpc_ops->get_adapter_status(slot, &(info->adapter_status));
401 result = pci_hp_change_slot_info(slot->hotplug_slot, info);
407 * Note: This function must be called with slot->lock held
409 static void handle_button_press_event(struct slot *p_slot)
411 struct controller *ctrl = p_slot->ctrl;
414 switch (p_slot->state) {
416 p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
418 p_slot->state = BLINKINGOFF_STATE;
419 info("PCI slot #%s - powering off due to button "
420 "press.\n", p_slot->name);
422 p_slot->state = BLINKINGON_STATE;
423 info("PCI slot #%s - powering on due to button "
424 "press.\n", p_slot->name);
426 /* blink green LED and turn off amber */
427 if (PWR_LED(ctrl->ctrlcap))
428 p_slot->hpc_ops->green_led_blink(p_slot);
429 if (ATTN_LED(ctrl->ctrlcap))
430 p_slot->hpc_ops->set_attention_status(p_slot, 0);
432 schedule_delayed_work(&p_slot->work, 5*HZ);
434 case BLINKINGOFF_STATE:
435 case BLINKINGON_STATE:
437 * Cancel if we are still blinking; this means that we
438 * press the attention again before the 5 sec. limit
439 * expires to cancel hot-add or hot-remove
441 info("Button cancel on Slot(%s)\n", p_slot->name);
442 dbg("%s: button cancel\n", __FUNCTION__);
443 cancel_delayed_work(&p_slot->work);
444 if (p_slot->state == BLINKINGOFF_STATE) {
445 if (PWR_LED(ctrl->ctrlcap))
446 p_slot->hpc_ops->green_led_on(p_slot);
448 if (PWR_LED(ctrl->ctrlcap))
449 p_slot->hpc_ops->green_led_off(p_slot);
451 if (ATTN_LED(ctrl->ctrlcap))
452 p_slot->hpc_ops->set_attention_status(p_slot, 0);
453 info("PCI slot #%s - action canceled due to button press\n",
455 p_slot->state = STATIC_STATE;
460 * Ignore if the slot is on power-on or power-off state;
461 * this means that the previous attention button action
462 * to hot-add or hot-remove is undergoing
464 info("Button ignore on Slot(%s)\n", p_slot->name);
465 update_slot_info(p_slot);
468 warn("Not a valid state\n");
474 * Note: This function must be called with slot->lock held
476 static void handle_surprise_event(struct slot *p_slot)
479 struct power_work_info *info;
481 info = kmalloc(sizeof(*info), GFP_KERNEL);
483 err("%s: Cannot allocate memory\n", __FUNCTION__);
486 info->p_slot = p_slot;
487 INIT_WORK(&info->work, pciehp_power_thread);
489 p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
491 p_slot->state = POWEROFF_STATE;
493 p_slot->state = POWERON_STATE;
495 queue_work(pciehp_wq, &info->work);
498 static void interrupt_event_handler(struct work_struct *work)
500 struct event_info *info = container_of(work, struct event_info, work);
501 struct slot *p_slot = info->p_slot;
502 struct controller *ctrl = p_slot->ctrl;
504 mutex_lock(&p_slot->lock);
505 switch (info->event_type) {
506 case INT_BUTTON_PRESS:
507 handle_button_press_event(p_slot);
509 case INT_POWER_FAULT:
510 if (!POWER_CTRL(ctrl->ctrlcap))
512 if (ATTN_LED(ctrl->ctrlcap))
513 p_slot->hpc_ops->set_attention_status(p_slot, 1);
514 if (PWR_LED(ctrl->ctrlcap))
515 p_slot->hpc_ops->green_led_off(p_slot);
517 case INT_PRESENCE_ON:
518 case INT_PRESENCE_OFF:
519 if (!HP_SUPR_RM(ctrl->ctrlcap))
521 dbg("Surprise Removal\n");
522 update_slot_info(p_slot);
523 handle_surprise_event(p_slot);
526 update_slot_info(p_slot);
529 mutex_unlock(&p_slot->lock);
534 int pciehp_enable_slot(struct slot *p_slot)
539 /* Check to see if (latch closed, card present, power off) */
540 mutex_lock(&p_slot->ctrl->crit_sect);
542 rc = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
543 if (rc || !getstatus) {
544 info("%s: no adapter on slot(%s)\n", __FUNCTION__,
546 mutex_unlock(&p_slot->ctrl->crit_sect);
549 if (MRL_SENS(p_slot->ctrl->ctrlcap)) {
550 rc = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
551 if (rc || getstatus) {
552 info("%s: latch open on slot(%s)\n", __FUNCTION__,
554 mutex_unlock(&p_slot->ctrl->crit_sect);
559 if (POWER_CTRL(p_slot->ctrl->ctrlcap)) {
560 rc = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
561 if (rc || getstatus) {
562 info("%s: already enabled on slot(%s)\n", __FUNCTION__,
564 mutex_unlock(&p_slot->ctrl->crit_sect);
569 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
571 rc = board_added(p_slot);
573 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
576 update_slot_info(p_slot);
578 mutex_unlock(&p_slot->ctrl->crit_sect);
583 int pciehp_disable_slot(struct slot *p_slot)
591 /* Check to see if (latch closed, card present, power on) */
592 mutex_lock(&p_slot->ctrl->crit_sect);
594 if (!HP_SUPR_RM(p_slot->ctrl->ctrlcap)) {
595 ret = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
596 if (ret || !getstatus) {
597 info("%s: no adapter on slot(%s)\n", __FUNCTION__,
599 mutex_unlock(&p_slot->ctrl->crit_sect);
604 if (MRL_SENS(p_slot->ctrl->ctrlcap)) {
605 ret = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
606 if (ret || getstatus) {
607 info("%s: latch open on slot(%s)\n", __FUNCTION__,
609 mutex_unlock(&p_slot->ctrl->crit_sect);
614 if (POWER_CTRL(p_slot->ctrl->ctrlcap)) {
615 ret = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
616 if (ret || !getstatus) {
617 info("%s: already disabled slot(%s)\n", __FUNCTION__,
619 mutex_unlock(&p_slot->ctrl->crit_sect);
623 * After turning power off, we must wait for at least
624 * 1 second before taking any action that relies on
625 * power having been removed from the slot/adapter.
630 ret = remove_board(p_slot);
631 update_slot_info(p_slot);
633 mutex_unlock(&p_slot->ctrl->crit_sect);
637 int pciehp_sysfs_enable_slot(struct slot *p_slot)
639 int retval = -ENODEV;
641 mutex_lock(&p_slot->lock);
642 switch (p_slot->state) {
643 case BLINKINGON_STATE:
644 cancel_delayed_work(&p_slot->work);
646 p_slot->state = POWERON_STATE;
647 mutex_unlock(&p_slot->lock);
648 retval = pciehp_enable_slot(p_slot);
649 mutex_lock(&p_slot->lock);
650 p_slot->state = STATIC_STATE;
653 info("Slot %s is already in powering on state\n",
656 case BLINKINGOFF_STATE:
658 info("Already enabled on slot %s\n", p_slot->name);
661 err("Not a valid state on slot %s\n", p_slot->name);
664 mutex_unlock(&p_slot->lock);
669 int pciehp_sysfs_disable_slot(struct slot *p_slot)
671 int retval = -ENODEV;
673 mutex_lock(&p_slot->lock);
674 switch (p_slot->state) {
675 case BLINKINGOFF_STATE:
676 cancel_delayed_work(&p_slot->work);
678 p_slot->state = POWEROFF_STATE;
679 mutex_unlock(&p_slot->lock);
680 retval = pciehp_disable_slot(p_slot);
681 mutex_lock(&p_slot->lock);
682 p_slot->state = STATIC_STATE;
685 info("Slot %s is already in powering off state\n",
688 case BLINKINGON_STATE:
690 info("Already disabled on slot %s\n", p_slot->name);
693 err("Not a valid state on slot %s\n", p_slot->name);
696 mutex_unlock(&p_slot->lock);