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 * After turning power off, we must wait for at least 1 second
179 * before taking any action that relies on power having been
180 * removed from the slot/adapter.
185 pslot->hpc_ops->green_led_off(pslot);
187 if (ATTN_LED(ctrl)) {
188 if (pslot->hpc_ops->set_attention_status(pslot, 1)) {
189 err("%s: Issue of Set Attention Led command failed\n",
197 * board_added - Called after a board has been added to the system.
198 * @p_slot: &slot where board is added
200 * Turns power on for the board.
203 static int board_added(struct slot *p_slot)
206 struct controller *ctrl = p_slot->ctrl;
208 dbg("%s: slot device, slot offset, hp slot = %d, %d ,%d\n",
209 __func__, p_slot->device,
210 ctrl->slot_device_offset, p_slot->hp_slot);
212 if (POWER_CTRL(ctrl)) {
214 retval = p_slot->hpc_ops->power_on_slot(p_slot);
220 p_slot->hpc_ops->green_led_blink(p_slot);
222 /* Wait for ~1 second */
225 /* Check link training status */
226 retval = p_slot->hpc_ops->check_lnk_status(ctrl);
228 err("%s: Failed to check link status\n", __func__);
229 set_slot_off(ctrl, p_slot);
233 /* Check for a power fault */
234 if (p_slot->hpc_ops->query_power_fault(p_slot)) {
235 dbg("%s: power fault detected\n", __func__);
236 retval = POWER_FAILURE;
240 retval = pciehp_configure_device(p_slot);
242 err("Cannot add device 0x%x:%x\n", p_slot->bus,
248 * Some PCI Express root ports require fixup after hot-plug operation.
251 pci_fixup_device(pci_fixup_final, ctrl->pci_dev);
253 p_slot->hpc_ops->green_led_on(p_slot);
258 set_slot_off(ctrl, p_slot);
263 * remove_board - Turns off slot and LEDs
264 * @p_slot: slot where board is being removed
266 static int remove_board(struct slot *p_slot)
269 struct controller *ctrl = p_slot->ctrl;
271 retval = pciehp_unconfigure_device(p_slot);
275 dbg("In %s, hp_slot = %d\n", __func__, p_slot->hp_slot);
277 if (POWER_CTRL(ctrl)) {
279 retval = p_slot->hpc_ops->power_off_slot(p_slot);
281 err("%s: Issue of Slot Disable command failed\n",
288 * After turning power off, we must wait for at least 1 second
289 * before taking any action that relies on power having been
290 * removed from the slot/adapter.
295 /* turn off Green LED */
296 p_slot->hpc_ops->green_led_off(p_slot);
301 struct power_work_info {
303 struct work_struct work;
307 * pciehp_power_thread - handle pushbutton events
308 * @work: &struct work_struct describing work to be done
310 * Scheduled procedure to handle blocking stuff for the pushbuttons.
311 * Handles all pending events and exits.
313 static void pciehp_power_thread(struct work_struct *work)
315 struct power_work_info *info =
316 container_of(work, struct power_work_info, work);
317 struct slot *p_slot = info->p_slot;
319 mutex_lock(&p_slot->lock);
320 switch (p_slot->state) {
322 mutex_unlock(&p_slot->lock);
323 dbg("%s: disabling bus:device(%x:%x)\n",
324 __func__, p_slot->bus, p_slot->device);
325 pciehp_disable_slot(p_slot);
326 mutex_lock(&p_slot->lock);
327 p_slot->state = STATIC_STATE;
330 mutex_unlock(&p_slot->lock);
331 if (pciehp_enable_slot(p_slot) &&
332 PWR_LED(p_slot->ctrl))
333 p_slot->hpc_ops->green_led_off(p_slot);
334 mutex_lock(&p_slot->lock);
335 p_slot->state = STATIC_STATE;
340 mutex_unlock(&p_slot->lock);
345 void pciehp_queue_pushbutton_work(struct work_struct *work)
347 struct slot *p_slot = container_of(work, struct slot, work.work);
348 struct power_work_info *info;
350 info = kmalloc(sizeof(*info), GFP_KERNEL);
352 err("%s: Cannot allocate memory\n", __func__);
355 info->p_slot = p_slot;
356 INIT_WORK(&info->work, pciehp_power_thread);
358 mutex_lock(&p_slot->lock);
359 switch (p_slot->state) {
360 case BLINKINGOFF_STATE:
361 p_slot->state = POWEROFF_STATE;
363 case BLINKINGON_STATE:
364 p_slot->state = POWERON_STATE;
369 queue_work(pciehp_wq, &info->work);
371 mutex_unlock(&p_slot->lock);
374 static int update_slot_info(struct slot *slot)
376 struct hotplug_slot_info *info;
379 info = kmalloc(sizeof(*info), GFP_KERNEL);
383 slot->hpc_ops->get_power_status(slot, &(info->power_status));
384 slot->hpc_ops->get_attention_status(slot, &(info->attention_status));
385 slot->hpc_ops->get_latch_status(slot, &(info->latch_status));
386 slot->hpc_ops->get_adapter_status(slot, &(info->adapter_status));
388 result = pci_hp_change_slot_info(slot->hotplug_slot, info);
394 * Note: This function must be called with slot->lock held
396 static void handle_button_press_event(struct slot *p_slot)
398 struct controller *ctrl = p_slot->ctrl;
401 switch (p_slot->state) {
403 p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
405 p_slot->state = BLINKINGOFF_STATE;
406 info("PCI slot #%s - powering off due to button "
407 "press.\n", p_slot->name);
409 p_slot->state = BLINKINGON_STATE;
410 info("PCI slot #%s - powering on due to button "
411 "press.\n", p_slot->name);
413 /* blink green LED and turn off amber */
415 p_slot->hpc_ops->green_led_blink(p_slot);
417 p_slot->hpc_ops->set_attention_status(p_slot, 0);
419 schedule_delayed_work(&p_slot->work, 5*HZ);
421 case BLINKINGOFF_STATE:
422 case BLINKINGON_STATE:
424 * Cancel if we are still blinking; this means that we
425 * press the attention again before the 5 sec. limit
426 * expires to cancel hot-add or hot-remove
428 info("Button cancel on Slot(%s)\n", p_slot->name);
429 dbg("%s: button cancel\n", __func__);
430 cancel_delayed_work(&p_slot->work);
431 if (p_slot->state == BLINKINGOFF_STATE) {
433 p_slot->hpc_ops->green_led_on(p_slot);
436 p_slot->hpc_ops->green_led_off(p_slot);
439 p_slot->hpc_ops->set_attention_status(p_slot, 0);
440 info("PCI slot #%s - action canceled due to button press\n",
442 p_slot->state = STATIC_STATE;
447 * Ignore if the slot is on power-on or power-off state;
448 * this means that the previous attention button action
449 * to hot-add or hot-remove is undergoing
451 info("Button ignore on Slot(%s)\n", p_slot->name);
452 update_slot_info(p_slot);
455 warn("Not a valid state\n");
461 * Note: This function must be called with slot->lock held
463 static void handle_surprise_event(struct slot *p_slot)
466 struct power_work_info *info;
468 info = kmalloc(sizeof(*info), GFP_KERNEL);
470 err("%s: Cannot allocate memory\n", __func__);
473 info->p_slot = p_slot;
474 INIT_WORK(&info->work, pciehp_power_thread);
476 p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
478 p_slot->state = POWEROFF_STATE;
480 p_slot->state = POWERON_STATE;
482 queue_work(pciehp_wq, &info->work);
485 static void interrupt_event_handler(struct work_struct *work)
487 struct event_info *info = container_of(work, struct event_info, work);
488 struct slot *p_slot = info->p_slot;
489 struct controller *ctrl = p_slot->ctrl;
491 mutex_lock(&p_slot->lock);
492 switch (info->event_type) {
493 case INT_BUTTON_PRESS:
494 handle_button_press_event(p_slot);
496 case INT_POWER_FAULT:
497 if (!POWER_CTRL(ctrl))
500 p_slot->hpc_ops->set_attention_status(p_slot, 1);
502 p_slot->hpc_ops->green_led_off(p_slot);
504 case INT_PRESENCE_ON:
505 case INT_PRESENCE_OFF:
506 if (!HP_SUPR_RM(ctrl))
508 dbg("Surprise Removal\n");
509 update_slot_info(p_slot);
510 handle_surprise_event(p_slot);
513 update_slot_info(p_slot);
516 mutex_unlock(&p_slot->lock);
521 int pciehp_enable_slot(struct slot *p_slot)
526 /* Check to see if (latch closed, card present, power off) */
527 mutex_lock(&p_slot->ctrl->crit_sect);
529 rc = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
530 if (rc || !getstatus) {
531 info("%s: no adapter on slot(%s)\n", __func__,
533 mutex_unlock(&p_slot->ctrl->crit_sect);
536 if (MRL_SENS(p_slot->ctrl)) {
537 rc = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
538 if (rc || getstatus) {
539 info("%s: latch open on slot(%s)\n", __func__,
541 mutex_unlock(&p_slot->ctrl->crit_sect);
546 if (POWER_CTRL(p_slot->ctrl)) {
547 rc = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
548 if (rc || getstatus) {
549 info("%s: already enabled on slot(%s)\n", __func__,
551 mutex_unlock(&p_slot->ctrl->crit_sect);
556 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
558 rc = board_added(p_slot);
560 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
563 update_slot_info(p_slot);
565 mutex_unlock(&p_slot->ctrl->crit_sect);
570 int pciehp_disable_slot(struct slot *p_slot)
578 /* Check to see if (latch closed, card present, power on) */
579 mutex_lock(&p_slot->ctrl->crit_sect);
581 if (!HP_SUPR_RM(p_slot->ctrl)) {
582 ret = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
583 if (ret || !getstatus) {
584 info("%s: no adapter on slot(%s)\n", __func__,
586 mutex_unlock(&p_slot->ctrl->crit_sect);
591 if (MRL_SENS(p_slot->ctrl)) {
592 ret = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
593 if (ret || getstatus) {
594 info("%s: latch open on slot(%s)\n", __func__,
596 mutex_unlock(&p_slot->ctrl->crit_sect);
601 if (POWER_CTRL(p_slot->ctrl)) {
602 ret = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
603 if (ret || !getstatus) {
604 info("%s: already disabled slot(%s)\n", __func__,
606 mutex_unlock(&p_slot->ctrl->crit_sect);
611 ret = remove_board(p_slot);
612 update_slot_info(p_slot);
614 mutex_unlock(&p_slot->ctrl->crit_sect);
618 int pciehp_sysfs_enable_slot(struct slot *p_slot)
620 int retval = -ENODEV;
622 mutex_lock(&p_slot->lock);
623 switch (p_slot->state) {
624 case BLINKINGON_STATE:
625 cancel_delayed_work(&p_slot->work);
627 p_slot->state = POWERON_STATE;
628 mutex_unlock(&p_slot->lock);
629 retval = pciehp_enable_slot(p_slot);
630 mutex_lock(&p_slot->lock);
631 p_slot->state = STATIC_STATE;
634 info("Slot %s is already in powering on state\n",
637 case BLINKINGOFF_STATE:
639 info("Already enabled on slot %s\n", p_slot->name);
642 err("Not a valid state on slot %s\n", p_slot->name);
645 mutex_unlock(&p_slot->lock);
650 int pciehp_sysfs_disable_slot(struct slot *p_slot)
652 int retval = -ENODEV;
654 mutex_lock(&p_slot->lock);
655 switch (p_slot->state) {
656 case BLINKINGOFF_STATE:
657 cancel_delayed_work(&p_slot->work);
659 p_slot->state = POWEROFF_STATE;
660 mutex_unlock(&p_slot->lock);
661 retval = pciehp_disable_slot(p_slot);
662 mutex_lock(&p_slot->lock);
663 p_slot->state = STATIC_STATE;
666 info("Slot %s is already in powering off state\n",
669 case BLINKINGON_STATE:
671 info("Already disabled on slot %s\n", p_slot->name);
674 err("Not a valid state on slot %s\n", p_slot->name);
677 mutex_unlock(&p_slot->lock);