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(struct slot *p_slot)
62 /* Attention Button Change */
63 dbg("pciehp: Attention button interrupt received.\n");
66 * Button pressed - See if need to TAKE ACTION!!!
68 info("Button pressed on Slot(%s)\n", p_slot->name);
69 event_type = INT_BUTTON_PRESS;
71 queue_interrupt_event(p_slot, event_type);
76 u8 pciehp_handle_switch_change(struct slot *p_slot)
82 dbg("pciehp: Switch interrupt received.\n");
84 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
89 info("Latch open on Slot(%s)\n", p_slot->name);
90 event_type = INT_SWITCH_OPEN;
95 info("Latch close on Slot(%s)\n", p_slot->name);
96 event_type = INT_SWITCH_CLOSE;
99 queue_interrupt_event(p_slot, event_type);
104 u8 pciehp_handle_presence_change(struct slot *p_slot)
109 /* Presence Change */
110 dbg("pciehp: Presence/Notify input change.\n");
112 /* Switch is open, assume a presence change
113 * Save the presence state
115 p_slot->hpc_ops->get_adapter_status(p_slot, &presence_save);
120 info("Card present on Slot(%s)\n", p_slot->name);
121 event_type = INT_PRESENCE_ON;
126 info("Card not present on Slot(%s)\n", p_slot->name);
127 event_type = INT_PRESENCE_OFF;
130 queue_interrupt_event(p_slot, event_type);
135 u8 pciehp_handle_power_fault(struct slot *p_slot)
140 dbg("pciehp: Power fault interrupt received.\n");
142 if ( !(p_slot->hpc_ops->query_power_fault(p_slot))) {
144 * power fault Cleared
146 info("Power fault cleared on Slot(%s)\n", p_slot->name);
147 event_type = INT_POWER_FAULT_CLEAR;
152 info("Power fault on Slot(%s)\n", p_slot->name);
153 event_type = INT_POWER_FAULT;
154 info("power fault bit %x set\n", 0);
157 queue_interrupt_event(p_slot, event_type);
162 /* The following routines constitute the bulk of the
163 hotplug controller logic
166 static void set_slot_off(struct controller *ctrl, struct slot * pslot)
168 /* turn off slot, turn on Amber LED, turn off Green LED if supported*/
169 if (POWER_CTRL(ctrl)) {
170 if (pslot->hpc_ops->power_off_slot(pslot)) {
171 err("%s: Issue of Slot Power Off command failed\n",
178 pslot->hpc_ops->green_led_off(pslot);
180 if (ATTN_LED(ctrl)) {
181 if (pslot->hpc_ops->set_attention_status(pslot, 1)) {
182 err("%s: Issue of Set Attention Led command failed\n",
190 * board_added - Called after a board has been added to the system.
191 * @p_slot: &slot where board is added
193 * Turns power on for the board.
196 static int board_added(struct slot *p_slot)
199 struct controller *ctrl = p_slot->ctrl;
201 dbg("%s: slot device, slot offset, hp slot = %d, %d ,%d\n",
202 __func__, p_slot->device,
203 ctrl->slot_device_offset, p_slot->hp_slot);
205 if (POWER_CTRL(ctrl)) {
207 retval = p_slot->hpc_ops->power_on_slot(p_slot);
213 p_slot->hpc_ops->green_led_blink(p_slot);
215 /* Wait for ~1 second */
218 /* Check link training status */
219 retval = p_slot->hpc_ops->check_lnk_status(ctrl);
221 err("%s: Failed to check link status\n", __func__);
222 set_slot_off(ctrl, p_slot);
226 /* Check for a power fault */
227 if (p_slot->hpc_ops->query_power_fault(p_slot)) {
228 dbg("%s: power fault detected\n", __func__);
229 retval = POWER_FAILURE;
233 retval = pciehp_configure_device(p_slot);
235 err("Cannot add device 0x%x:%x\n", p_slot->bus,
241 * Some PCI Express root ports require fixup after hot-plug operation.
244 pci_fixup_device(pci_fixup_final, ctrl->pci_dev);
246 p_slot->hpc_ops->green_led_on(p_slot);
251 set_slot_off(ctrl, p_slot);
256 * remove_board - Turns off slot and LEDs
257 * @p_slot: slot where board is being removed
259 static int remove_board(struct slot *p_slot)
262 struct controller *ctrl = p_slot->ctrl;
264 retval = pciehp_unconfigure_device(p_slot);
268 dbg("In %s, hp_slot = %d\n", __func__, p_slot->hp_slot);
270 if (POWER_CTRL(ctrl)) {
272 retval = p_slot->hpc_ops->power_off_slot(p_slot);
274 err("%s: Issue of Slot Disable command failed\n",
281 /* turn off Green LED */
282 p_slot->hpc_ops->green_led_off(p_slot);
287 struct power_work_info {
289 struct work_struct work;
293 * pciehp_power_thread - handle pushbutton events
294 * @work: &struct work_struct describing work to be done
296 * Scheduled procedure to handle blocking stuff for the pushbuttons.
297 * Handles all pending events and exits.
299 static void pciehp_power_thread(struct work_struct *work)
301 struct power_work_info *info =
302 container_of(work, struct power_work_info, work);
303 struct slot *p_slot = info->p_slot;
305 mutex_lock(&p_slot->lock);
306 switch (p_slot->state) {
308 mutex_unlock(&p_slot->lock);
309 dbg("%s: disabling bus:device(%x:%x)\n",
310 __func__, p_slot->bus, p_slot->device);
311 pciehp_disable_slot(p_slot);
312 mutex_lock(&p_slot->lock);
313 p_slot->state = STATIC_STATE;
316 mutex_unlock(&p_slot->lock);
317 if (pciehp_enable_slot(p_slot) &&
318 PWR_LED(p_slot->ctrl))
319 p_slot->hpc_ops->green_led_off(p_slot);
320 mutex_lock(&p_slot->lock);
321 p_slot->state = STATIC_STATE;
326 mutex_unlock(&p_slot->lock);
331 void pciehp_queue_pushbutton_work(struct work_struct *work)
333 struct slot *p_slot = container_of(work, struct slot, work.work);
334 struct power_work_info *info;
336 info = kmalloc(sizeof(*info), GFP_KERNEL);
338 err("%s: Cannot allocate memory\n", __func__);
341 info->p_slot = p_slot;
342 INIT_WORK(&info->work, pciehp_power_thread);
344 mutex_lock(&p_slot->lock);
345 switch (p_slot->state) {
346 case BLINKINGOFF_STATE:
347 p_slot->state = POWEROFF_STATE;
349 case BLINKINGON_STATE:
350 p_slot->state = POWERON_STATE;
355 queue_work(pciehp_wq, &info->work);
357 mutex_unlock(&p_slot->lock);
360 static int update_slot_info(struct slot *slot)
362 struct hotplug_slot_info *info;
365 info = kmalloc(sizeof(*info), GFP_KERNEL);
369 slot->hpc_ops->get_power_status(slot, &(info->power_status));
370 slot->hpc_ops->get_attention_status(slot, &(info->attention_status));
371 slot->hpc_ops->get_latch_status(slot, &(info->latch_status));
372 slot->hpc_ops->get_adapter_status(slot, &(info->adapter_status));
374 result = pci_hp_change_slot_info(slot->hotplug_slot, info);
380 * Note: This function must be called with slot->lock held
382 static void handle_button_press_event(struct slot *p_slot)
384 struct controller *ctrl = p_slot->ctrl;
387 switch (p_slot->state) {
389 p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
391 p_slot->state = BLINKINGOFF_STATE;
392 info("PCI slot #%s - powering off due to button "
393 "press.\n", p_slot->name);
395 p_slot->state = BLINKINGON_STATE;
396 info("PCI slot #%s - powering on due to button "
397 "press.\n", p_slot->name);
399 /* blink green LED and turn off amber */
401 p_slot->hpc_ops->green_led_blink(p_slot);
403 p_slot->hpc_ops->set_attention_status(p_slot, 0);
405 schedule_delayed_work(&p_slot->work, 5*HZ);
407 case BLINKINGOFF_STATE:
408 case BLINKINGON_STATE:
410 * Cancel if we are still blinking; this means that we
411 * press the attention again before the 5 sec. limit
412 * expires to cancel hot-add or hot-remove
414 info("Button cancel on Slot(%s)\n", p_slot->name);
415 dbg("%s: button cancel\n", __func__);
416 cancel_delayed_work(&p_slot->work);
417 if (p_slot->state == BLINKINGOFF_STATE) {
419 p_slot->hpc_ops->green_led_on(p_slot);
422 p_slot->hpc_ops->green_led_off(p_slot);
425 p_slot->hpc_ops->set_attention_status(p_slot, 0);
426 info("PCI slot #%s - action canceled due to button press\n",
428 p_slot->state = STATIC_STATE;
433 * Ignore if the slot is on power-on or power-off state;
434 * this means that the previous attention button action
435 * to hot-add or hot-remove is undergoing
437 info("Button ignore on Slot(%s)\n", p_slot->name);
438 update_slot_info(p_slot);
441 warn("Not a valid state\n");
447 * Note: This function must be called with slot->lock held
449 static void handle_surprise_event(struct slot *p_slot)
452 struct power_work_info *info;
454 info = kmalloc(sizeof(*info), GFP_KERNEL);
456 err("%s: Cannot allocate memory\n", __func__);
459 info->p_slot = p_slot;
460 INIT_WORK(&info->work, pciehp_power_thread);
462 p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
464 p_slot->state = POWEROFF_STATE;
466 p_slot->state = POWERON_STATE;
468 queue_work(pciehp_wq, &info->work);
471 static void interrupt_event_handler(struct work_struct *work)
473 struct event_info *info = container_of(work, struct event_info, work);
474 struct slot *p_slot = info->p_slot;
475 struct controller *ctrl = p_slot->ctrl;
477 mutex_lock(&p_slot->lock);
478 switch (info->event_type) {
479 case INT_BUTTON_PRESS:
480 handle_button_press_event(p_slot);
482 case INT_POWER_FAULT:
483 if (!POWER_CTRL(ctrl))
486 p_slot->hpc_ops->set_attention_status(p_slot, 1);
488 p_slot->hpc_ops->green_led_off(p_slot);
490 case INT_PRESENCE_ON:
491 case INT_PRESENCE_OFF:
492 if (!HP_SUPR_RM(ctrl))
494 dbg("Surprise Removal\n");
495 update_slot_info(p_slot);
496 handle_surprise_event(p_slot);
499 update_slot_info(p_slot);
502 mutex_unlock(&p_slot->lock);
507 int pciehp_enable_slot(struct slot *p_slot)
512 /* Check to see if (latch closed, card present, power off) */
513 mutex_lock(&p_slot->ctrl->crit_sect);
515 rc = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
516 if (rc || !getstatus) {
517 info("%s: no adapter on slot(%s)\n", __func__,
519 mutex_unlock(&p_slot->ctrl->crit_sect);
522 if (MRL_SENS(p_slot->ctrl)) {
523 rc = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
524 if (rc || getstatus) {
525 info("%s: latch open on slot(%s)\n", __func__,
527 mutex_unlock(&p_slot->ctrl->crit_sect);
532 if (POWER_CTRL(p_slot->ctrl)) {
533 rc = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
534 if (rc || getstatus) {
535 info("%s: already enabled on slot(%s)\n", __func__,
537 mutex_unlock(&p_slot->ctrl->crit_sect);
542 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
544 rc = board_added(p_slot);
546 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
549 update_slot_info(p_slot);
551 mutex_unlock(&p_slot->ctrl->crit_sect);
556 int pciehp_disable_slot(struct slot *p_slot)
564 /* Check to see if (latch closed, card present, power on) */
565 mutex_lock(&p_slot->ctrl->crit_sect);
567 if (!HP_SUPR_RM(p_slot->ctrl)) {
568 ret = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
569 if (ret || !getstatus) {
570 info("%s: no adapter on slot(%s)\n", __func__,
572 mutex_unlock(&p_slot->ctrl->crit_sect);
577 if (MRL_SENS(p_slot->ctrl)) {
578 ret = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
579 if (ret || getstatus) {
580 info("%s: latch open on slot(%s)\n", __func__,
582 mutex_unlock(&p_slot->ctrl->crit_sect);
587 if (POWER_CTRL(p_slot->ctrl)) {
588 ret = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
589 if (ret || !getstatus) {
590 info("%s: already disabled slot(%s)\n", __func__,
592 mutex_unlock(&p_slot->ctrl->crit_sect);
597 ret = remove_board(p_slot);
598 update_slot_info(p_slot);
600 mutex_unlock(&p_slot->ctrl->crit_sect);
604 int pciehp_sysfs_enable_slot(struct slot *p_slot)
606 int retval = -ENODEV;
608 mutex_lock(&p_slot->lock);
609 switch (p_slot->state) {
610 case BLINKINGON_STATE:
611 cancel_delayed_work(&p_slot->work);
613 p_slot->state = POWERON_STATE;
614 mutex_unlock(&p_slot->lock);
615 retval = pciehp_enable_slot(p_slot);
616 mutex_lock(&p_slot->lock);
617 p_slot->state = STATIC_STATE;
620 info("Slot %s is already in powering on state\n",
623 case BLINKINGOFF_STATE:
625 info("Already enabled on slot %s\n", p_slot->name);
628 err("Not a valid state on slot %s\n", p_slot->name);
631 mutex_unlock(&p_slot->lock);
636 int pciehp_sysfs_disable_slot(struct slot *p_slot)
638 int retval = -ENODEV;
640 mutex_lock(&p_slot->lock);
641 switch (p_slot->state) {
642 case BLINKINGOFF_STATE:
643 cancel_delayed_work(&p_slot->work);
645 p_slot->state = POWEROFF_STATE;
646 mutex_unlock(&p_slot->lock);
647 retval = pciehp_disable_slot(p_slot);
648 mutex_lock(&p_slot->lock);
649 p_slot->state = STATIC_STATE;
652 info("Slot %s is already in powering off state\n",
655 case BLINKINGON_STATE:
657 info("Already disabled on slot %s\n", p_slot->name);
660 err("Not a valid state on slot %s\n", p_slot->name);
663 mutex_unlock(&p_slot->lock);